Skip to content

Google Gemini not supporting native structured output - instead always using tool calling method #8585

Description

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

// Define the output format selection schema
const OutputFormatSelectionSchema = z.object({
  query: z
    .string()
    .describe('The search query'),
});

const model = ChatGoogle({
          model: 'gemini-2.5-flash-lite',
          temperature: 0,
        });

  const structuredLlm = withStructuredOutput(model, OutputFormatSelectionSchema);

const result = await structuredLlm.invoke("Generate a search query")

Error Message and Stack Trace (if applicable)

No response

Description

With structuredOutput is defaulting to extract tool because of missing logic for Gemini native structured out.
This usually results in much worse results for JSON generation.

To get native json generation we would need to pass responseMimeType and responseSchema to the API request.
https://ai.google.dev/gemini-api/docs/structured-output#javascript

System Info

Node: v23.11.0
Mac OS X

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 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: ^3.25.32
langsmith: ^0.3.33

published a week ago by davidduong david@duong.cz

Metadata

Metadata

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