langchainjs icon indicating copy to clipboard operation
langchainjs copied to clipboard

ChatGoogleGenerativeAI: Invalid JSON payload received

Open m-khashaba4 opened this issue 10 months ago • 4 comments

Checked other resources

  • [x] I added a very descriptive title to this issue.
  • [x] I searched the LangChain.js documentation with the integrated search.
  • [x] I used the GitHub search to find a similar question and didn't find it.
  • [x] I am sure that this is a bug in LangChain.js rather than my code.
  • [x] The bug is not resolved by updating to the latest stable version of LangChain (or the specific integration package).

Example Code


export async function init(commit: string) {
    const model = new ChatGoogleGenerativeAI({
        model: "gemini-2.0-flash",
        temperature: 1.0,
    });
    
    return (
        await PullFromLangSmith<typeof ModelInput, typeof ModelOu1put>(
            `node:${commit}`,
            ModelInput,
            ModelOu1put,
            model,
            true
        )
    )
} 

Error Message and Stack Trace (if applicable)

{"status":400,"statusText":"Bad Request","errorDetails":[{"@type":"type.googleapis.com/google.rpc.BadRequest","fieldViolations":[{"field":"tools[0].function_declarations[0].parameters","description":"Invalid JSON payload received. Unknown name \"title\" at 'tools[0].function_declarations[0].parameters': Cannot find field."},{"field":"tools[0].function_declarations[0].parameters","description":"Invalid JSON payload received. Unknown name \"additionalProperties\" at 'tools[0].function_declarations[0].parameters': Cannot find field."}]}],"attemptNumber":1,"retriesLeft":6},"trace":"Error: [GoogleGenerativeAI Error]: Error fetching from https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:streamGenerateContent?alt=sse: [400 Bad Request] Invalid JSON payload received. Unknown name \"title\" at 'tools[0].function_declarations[0].parameters': Cannot find field.\nInvalid JSON payload received. Unknown name \"additionalProperties\" at 'tools[0].function_declarations[0].parameters': Cannot find field. [{\"@type\":\"type.googleapis.com/google.rpc.BadRequest\",\"fieldViolations\":[{\"field\":\"tools[0].function_declarations[0].parameters\",\"description\":\"Invalid JSON payload received. Unknown name \\\"title\\\" at 'tools[0].function_declarations[0].parameters': Cannot find field.\"},{\"field\":\"tools[0].function_declarations[0].parameters\",\"description\":\"Invalid JSON payload received. Unknown name \\\"additionalProperties\\\" at 'tools[0].function_declarations[0].parameters': Cannot find field.\"}]}]\n at handleResponseNotOk (../node_modules/@google/generative-ai/dist/index.js:414:11)\n at process.processTicksAndRejections (node:internal/process/task_queues:105:5)\n at async makeRequest (../node_modules/@google/generative-ai/dist/index.js:387:9)\n at async generateContentStream (../node_modules/@google/generative-ai/dist/index.js:827:22)\n at async ../node_modules/@langchain/google-genai/dist/chat_models.cjs:681:32\n at async RetryOperation._fn (../node_modules/p-retry/index.js:50:12)","errorMessage":"[GoogleGenerativeAI Error]: Error fetching from https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:streamGenerateContent?alt=sse: [400 Bad Request] Invalid JSON payload received. Unknown name \"title\" at 'tools[0].function_declarations[0].parameters': Cannot find field.\nInvalid JSON payload received. Unknown name \"additionalProperties\" at 'tools[0].function_declarations[0].parameters': Cannot find field. [{\"@type\":\"type.googleapis.com/google.rpc.BadRequest\",\"fieldViolations\":[{\"field\":\"tools[0].function_declarations[0].parameters\",\"description\":\"Invalid JSON payload received. Unknown name \\\"title\\\" at 'tools[0].function_declarations[0].parameters': Cannot find field.\"},{\"field\":\"tools[0].function_declarations[0].parameters\",\"description\":\"Invalid JSON payload received. Unknown name \\\"additionalProperties\\\" at 'tools[0].function_declarations[0].parameters': Cannot find field.\"}]}]"}

Description

when invoking i get this error

System Info

"@langchain/google-genai": "^0.1.8",

m-khashaba4 avatar Feb 17 '25 20:02 m-khashaba4

@m-khashaba4 Hello there! I'm here to help you with any bugs, questions, or contributions. Let's tackle this together!

I found a similar open issue regarding the GoogleGenerativeAIError related to the JSON payload when using ChatGoogleGenerativeAI with LangChain.js. The suggested solution is to ensure that the JSON payload you are sending is correctly formatted and adheres to the expected structure. The payload should be an array of BaseMessage objects, and each BaseMessage object should be correctly instantiated, such as using HumanMessage for human inputs [1].

In your case, the error message indicates that there are unknown fields, "title" and "additionalProperties," in the JSON payload. These fields are not recognized by the API, which is causing the error. You should review the JSON payload being sent to ensure it only includes valid fields as expected by the ChatGoogleGenerativeAI model.

If you need more information on the valid fields for the 'parameters' in the JSON payload, they include fields like model, temperature, maxOutputTokens, topP, topK, stopSequences, safetySettings, apiKey, apiVersion, baseUrl, streaming, json, and convertSystemMessageToHumanContent [2]. Make sure your payload aligns with these specifications.

To continue talking to Dosu, mention @dosu.


Help Dosu learn! Give it feedback: Great Response | Irrelevant Answer | Incorrect Sources | Too Verbose | Hallucination | Bug Report | Other

dosubot[bot] avatar Feb 17 '25 21:02 dosubot[bot]

I see the same error. I am using createReactAgent to call the tools.

gurvinder-dhillon avatar Apr 02 '25 03:04 gurvinder-dhillon

I found the fix. In my case, there is a field in zod tool schema which was using a default value for one the tool param. Had to remove the default value from the tool param.

gurvinder-dhillon avatar Apr 02 '25 03:04 gurvinder-dhillon

in my case that was the fix we had to pipe manually

const promptTemplate = ChatPromptTemplate.fromMessages<z.infer<typeof ModelInput>>(
        fromLangSmith.lc_kwargs.first.promptMessages
    )
    const gmModel = (model as ChatGoogleGenerativeAI).withStructuredOutput(ModelOu1put)
    return (
        promptTemplate.pipe(gmModel)
    )

m-khashaba4 avatar Apr 02 '25 07:04 m-khashaba4

想要 Work,直接在 bind_tools 之前,删除掉 tool 的 $schema 属性就行,像这样
del tool.args_schema["$schema"]

SudoUserReal avatar May 08 '25 12:05 SudoUserReal