Support Gemini Structured outputs #6846
fernando-deka
started this conversation in
Ideas
Replies: 2 comments
-
I've just run into this limitation too. I can sort of see what needs to be done to support...
It's not trivial to support but shouldn't be too hard for the author either. Hope this module is still supported and someone will pick up |
Beta Was this translation helpful? Give feedback.
0 replies
-
Here is a holy hack to make it work! // Custom ChatVertexAI with schema support
import { ChatVertexAI } from '@langchain/google-vertexai'
import logger from '../logger'
export class CustomChatVertexAI extends ChatVertexAI {
private responseSchema?: any
private originalFormatData: any
constructor(
fields: ConstructorParameters<typeof ChatVertexAI>[0] & {
responseSchema?: any
}
) {
const { responseSchema, ...rest } = fields
super(rest)
this.responseSchema = responseSchema
// Store the original formatData method
this.originalFormatData = this.connection.formatData
// Replace formatData with our interceptor
this.connection.formatData = async (...args: any[]) => {
const result = await this.originalFormatData.apply(this.connection, args)
logger.info('Connection formatData result:', result)
if (result.generationConfig.responseMimeType === 'application/json') {
result.generationConfig.responseSchema = this.responseSchema
}
return result
}
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Checked
Feature request
Gemini models support structured output JSON schemas:
https://cloud.google.com/vertex-ai/generative-ai/docs/multimodal/control-generated-output#generativeaionvertexai_gemini_controlled_generation_response_schema-drest
Currently langchain only supports setting the
responseMimeType
to json but does not set theresponseSchema
to ensure that the JSON follows the given schema.Motivation
Currently when using the built in
.withStructuredOutput
the model usually fails with complex schemas. With this feature the reliability would increase a lot.Proposal (If applicable)
No response
Beta Was this translation helpful? Give feedback.
All reactions