-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
ChatGoogleGenerativeAI: Invalid JSON payload received #5779
Comments
Hello @netwrkx! I'm here to assist you with any bugs, questions, or contribution inquiries you may have. The error you're encountering is due to the Here's how you can modify your
Here's the updated code: class QuestionGenTool extends StructuredTool {
questionSchema = z.object({
question: z.string().describe("Question text"),
type: z.enum(['singleChoice', 'multiChoice']).describe("Question type"),
options: z.array(z.string()).describe("List of possible answers"),
correctAnswer: z.string().optional().describe("correct answer from the possible answers"),
});
schema = z.array(this.questionSchema).describe("Array of question objects");
name = "question_gen_tool";
description = "useful for when you need to generate questions on a subject.";
async _call(questions) {
try {
const validatedQuestions = this.schema.safeParse(questions);
if (!validatedQuestions.success) {
throw new Error("Invalid question schema: " + validatedQuestions.error.issues.join(", "));
}
return "question_gen_tool";
} catch (error) {
console.error("Error in QuestionGenTool._call:", error);
throw error; // Re-throw the error for handling
}
}
}
const tool = new QuestionGenTool();
const modelWithTools = googleModel.withStructuredOutput(tool.schema, {
name: tool.name,
});
const response = async () => await modelWithTools.invoke([
[
"human",
"Create an array of 8 questions with 5 options(4 incorrect answer and 1 correct answer) for an interview with a science fiction author.",
],
]); This should resolve the |
CC @bracesproul |
@netwrkx version |
@bracesproul Thanks. version |
@bracesproul I'm still getting it when I run with Gemini.
Same thing works with openai |
Checked other resources
Example Code
javascript ...
Error Message and Stack Trace (if applicable)
Description
i'm trying to use langchain library to generate questions in json array format
Followed the example in this link
Generating json object works but json array returns error.
System Info
"@langchain/community": "^0.2.11",
"@langchain/google-genai": "^0.0.17",
"langchain": "^0.2.5",
"zod": "^3.23.8"
Node v18.3.0
The text was updated successfully, but these errors were encountered: