Skip to content

Gemini-2.0-flash rejects function schemas containing propertyNames, causing 400 Bad Request #8584

@milosv-vtool

Description

@milosv-vtool

Checked other resources

  • This is a bug, not a usage question. For questions, please use the LangChain Forum (https://forum.langchain.com/).
  • I added a very descriptive title to this issue.
  • I searched the LangChain.js documentation with the integrated search.
  • I used the GitHub search to find a similar question and didn't find it.
  • I am sure that this is a bug in LangChain.js rather than my code.
  • The bug is not resolved by updating to the latest stable version of LangChain (or the specific integration package).

Example Code

import { ChatPromptTemplate } from '@langchain/core/prompts';
import { tool } from '@langchain/core/tools';
import { ChatGoogleGenerativeAI } from '@langchain/google-genai';
import { AgentExecutor, createToolCallingAgent } from 'langchain/agents';
import z from 'zod';

const MagicNumberSchema = z.object({
    input: z.number().describe('Input number to apply the magic function to.'),
    someVeryComplexField: z.record(z.string(), z.string()).optional().describe(
        'A complex field that can hold various string key-value pairs.',
    ),
});

const prompt = ChatPromptTemplate.fromMessages([
    ['system', 'You are a helpful assistant'],
    ['placeholder', '{chat_history}'],
    ['human', '{input}'],
    ['placeholder', '{agent_scratchpad}'],
]);

const llm = new ChatGoogleGenerativeAI({
    model: 'gemini-2.0-flash',
    apiKey: Deno.env.get('GOOGLE_GENERATIVE_AI_API_KEY'),
    baseUrl: Deno.env.get('GOOGLE_GENERATIVE_AI_API_URL') || 'https://generativelanguage.googleapis.com',
    streaming: false,
});

const magicTool = tool(
    ({ input }) => {
        return `${input + 2}`;
    },
    {
        name: 'magic_function',
        description: 'Applies a magic function to an input.',
        schema: MagicNumberSchema,
    },
);

const agent = createToolCallingAgent({
    llm,
    tools: [magicTool],
    prompt,
});

const agentExecutor = new AgentExecutor({
    agent,
    tools: [magicTool],
});

const response = await agentExecutor.invoke({
    input: 'Hello, what is 2 + 2?',
});

console.log(response.output); // Should log the result of the magic function applied to the input instead it throws an error

Error Message and Stack Trace (if applicable)

error: Uncaught (in promise) 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 "propertyNames" at 'tools[0].function_declarations[0].parameters.properties[1].value': Cannot find field. [{"@type":"type.googleapis.com/google.rpc.BadRequest","fieldViolations":[{"field":"tools[0].function_declarations[0].parameters.properties[1].value","description":"Invalid JSON payload received. Unknown name \"propertyNames\" at 'tools[0].function_declarations[0].parameters.properties[1].value': Cannot find field."}]}]
    at handleResponseNotOk (/node_modules/.deno/@google+generative-ai@0.24.1/node_modules/@google/generative-ai/dist/index.mjs:432:11)
    at eventLoopTick (ext:core/01_core.js:178:7)
    at async makeRequest (/node_modules/.deno/@google+generative-ai@0.24.1/node_modules/@google/generative-ai/dist/index.mjs:401:9)
    at async generateContentStream (/node_modules/.deno/@google+generative-ai@0.24.1/node_modules/@google/generative-ai/dist/index.mjs:860:22)
    at async /node_modules/.deno/@langchain+google-genai@0.2.16/node_modules/@langchain/google-genai/dist/chat_models.js:690:32
    at async RetryOperation._fn (/node_modules/.deno/p-retry@4.6.2/node_modules/p-retry/index.js:50:12)

Description

When using @langchain/google-genai with Gemini-2.0-flash, any Zod schema that includes dynamic keys (e.g., z.record(...)) generates a JSON schema containing the propertyNames keyword. The Gemini API strictly validates the schema and returns a 400 Bad Request with the message.

System Info

pnpm info langchain

langchain@0.3.30 | MIT | deps: 11 | versions: 325
Typescript bindings for langchain
https://github.com/langchain-ai/langchainjs/tree/main/langchain/

keywords: llm, ai, gpt3, chain, prompt, prompt engineering, chatgpt, machine learning, ml, openai, embeddings, vectorstores

dist
.tarball: https://registry.npmjs.org/langchain/-/langchain-0.3.30.tgz
.shasum: 9fc0467007009dc5e799d1f8fb9dd1137654e387
.integrity: sha512-UyVsfwHDpHbrnWrjWuhJHqi8Non+Zcsf2kdpDTqyJF8NXrHBOpjdHT5LvPuW9fnE7miDTWf5mLcrWAGZgcrznQ==
.unpackedSize: 2.9 MB

dependencies:
@langchain/openai: >=0.1.0 <0.7.0        js-yaml: ^4.1.0                          openapi-types: ^12.1.3                   yaml: ^2.2.1                             
@langchain/textsplitters: >=0.0.0 <0.2.0 jsonpointer: ^5.0.1                      p-retry: 4                               zod: ^3.25.32                            
js-tiktoken: ^1.0.12                     langsmith: ^0.3.33                       uuid: ^10.0.0                            

maintainers:
- nfcampos <nuno@boringbits.io>
- jacoblee93 <jacoblee93@gmail.com>
- andrewnguonly <andrewnguonly@gmail.com>
- davidduong <david@duong.cz>
- hntrl <hunter@hntrl.io>
- hwchase17 <hw.chase.17@gmail.com>
- basproul <braceasproul@gmail.com>

dist-tags:
latest: 0.3.30                            tag-for-publishing-older-releases: 0.2.20 next: 0.3.2-rc.0                          

published a week ago by davidduong <david@duong.cz>

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions