-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Gemini JSON Mode Not Working #7172
Comments
Hey @dcb6! I'm here to help you with any bugs, questions, or contributions. If you need any assistance, feel free to ask. Dosu The issue you're encountering with the Gemini JSON mode in LangChain.js is due to the fact that JSON mode is not supported by the There is ongoing development to support JSON mode, as indicated by test cases demonstrating its use. However, according to the latest documentation, JSON mode is still not supported [2][3]. For structured output, you can use the |
To achieve structured output with the Gemini model in LangChain.js without using JSON mode, you can use the import { z } from "zod";
import { ChatOpenAI } from "@langchain/openai";
// Define the schema using Zod
const ResponseFormatter = z.object({
joke: z.string().describe("A joke"),
});
// Initialize the model
const model = new ChatOpenAI({
modelName: "gpt-4",
temperature: 0,
});
// Bind the schema to the model
const modelWithStructure = model.withStructuredOutput(ResponseFormatter);
// Invoke the model to produce structured output
const structuredOutput = await modelWithStructure.invoke("Tell me a joke");
// Get back the object
console.log(structuredOutput);
// { joke: "Why don't scientists trust atoms? Because they make up everything!" } This approach allows you to enforce structured output without relying on JSON mode, which is currently not supported by the Gemini model. If you encounter errors related to function calling, ensure that your model and schema setup are correct and compatible with the function calling method [1]. |
this is so broken and can't believe no one is fixing it yet, switched to generateObject and never regret it! |
Checked other resources
Example Code
Error Message and Stack Trace (if applicable)
Error: ChatGoogleGenerativeAI only supports "functionCalling" as a method.
at ChatGoogleGenerativeAI.withStructuredOutput
Description
Despite this closed ticket that seems to mention support for Gemini structured outputs via JSON mode, I still get that error. It seems maybe PR that closed it just implemented support for a general JSON response type?
System Info
@langchain/openai: >=0.1.0 <0.4.0 openapi-types: ^12.1.3
@langchain/textsplitters: >=0.0.0 <0.2.0 p-retry: 4
js-tiktoken: ^1.0.12 uuid: ^10.0.0
js-yaml: ^4.1.0 yaml: ^2.2.1
jsonpointer: ^5.0.1 zod-to-json-schema: ^3.22.3
langsmith: ^0.2.0 zod: ^3.22.4
The text was updated successfully, but these errors were encountered: