Skip to content

fix(google): tool_choice none with empty tools, response metadata, structured output format#10438

Open
pawel-twardziak wants to merge 4 commits intolangchain-ai:mainfrom
pawel-twardziak:fix/google-tool-choice-none-response-metadata
Open

fix(google): tool_choice none with empty tools, response metadata, structured output format#10438
pawel-twardziak wants to merge 4 commits intolangchain-ai:mainfrom
pawel-twardziak:fix/google-tool-choice-none-response-metadata

Conversation

@pawel-twardziak
Copy link
Contributor

@pawel-twardziak pawel-twardziak commented Mar 17, 2026

Changes

1. tool_choice: "none" with empty tools (converters/tools.ts)

convertToolChoiceToGeminiConfig had a guard if (!hasTools || toolChoice === undefined) return undefined that discarded tool_choice: "none" whenever tools were absent. This meant bindTools([], { tool_choice: 'none' }) silently sent no toolConfig to the API, allowing models like gemini-3-flash-preview to hallucinate function calls.

The fix splits the guard so "none" bypasses the !hasTools check - it is the only mode that is semantically meaningful without tool declarations. All other modes ("auto", "any", etc.) still return undefined when no tools are present.

2. response_metadata.model missing (chat_models/base.ts)

Neither the invoke path nor the streaming path included the model name in response_metadata. Other providers (OpenAI, Anthropic, and now @langchain/google-genai) expose response_metadata.model. The fix adds model: data.modelVersion (invoke) and model: chunk.modelVersion (stream) from the API response.

3. ls_structured_output_format missing (chat_models/base.ts)

withStructuredOutput did not set the ls_structured_output_format config key, so LangSmith could not track which structured output strategy or schema was used. The fix adds ls_structured_output_format: { kwargs: { method }, schema: toJsonSchema(schema) } to both the "jsonSchema" and "functionCalling" paths, matching what @langchain/google-genai already does.

Tests

  • New unit test for convertToolChoiceToGeminiConfig("none", false) - verifies NONE config is returned even without tools.
  • Updated existing test description to clarify it covers hasTools=false with non-none choices.

Fixes #10432

@changeset-bot
Copy link

changeset-bot bot commented Mar 17, 2026

🦋 Changeset detected

Latest commit: cc9f39d

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@langchain/google Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug(@langchain/google) tool_choice not transformed and sent out in the body of generateContent if tools array is empty.

1 participant