Skip to content

fix(google-common): pass structured objects directly in FunctionResponse#10441

Open
Jackjin (jackjin1997) wants to merge 1 commit intolangchain-ai:mainfrom
jackjin1997:fix/google-tool-message-structured-content
Open

fix(google-common): pass structured objects directly in FunctionResponse#10441
Jackjin (jackjin1997) wants to merge 1 commit intolangchain-ai:mainfrom
jackjin1997:fix/google-tool-message-structured-content

Conversation

@jackjin1997
Copy link

Summary

  • Fix toolMessageToContent in @langchain/google-common to pass structured objects directly to the Gemini API's FunctionResponse.response field instead of double-serializing them
  • The Gemini API accepts any JSON object as the response (Struct format), but the previous code always wrapped parsed content in { content: parsed }, which added an unnecessary nesting layer
  • Now correctly handles: JSON string content (parsed and passed directly), plain string content (wrapped in { result } ), JSON arrays, and ContentBlock arrays

Why

The Gemini API docs specify that FunctionResponse.response accepts any arbitrary JSON object directly. The previous implementation unnecessarily stringified and re-parsed content, and wrapped results in { content: ... } which doesn't match the API's expected format. This caused issues when tools return structured JSON — the model received stringified JSON instead of the actual object structure.

Test plan

  • Added unit tests for JSON string content → parsed object in response
  • Added unit tests for plain string content → { result: string }
  • Added unit tests for JSON array content → { result: array }
  • All 43 existing + new tests pass

Fixes #10439

This PR was developed with assistance from an AI coding agent.

@changeset-bot
Copy link

changeset-bot bot commented Mar 17, 2026

🦋 Changeset detected

Latest commit: 5fa232b

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

This PR includes changesets to release 6 packages
Name Type
@langchain/google-common Patch
@langchain/google-gauth Patch
@langchain/google-vertexai-web Patch
@langchain/google-vertexai Patch
@langchain/google-webauth Patch
@langchain/google-genai 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

Parse JSON string content and pass the resulting object directly as
FunctionResponse.response instead of wrapping in { content: ... }.
Plain strings and arrays are wrapped in { result: ... }.
@jackjin1997 Jackjin (jackjin1997) force-pushed the fix/google-tool-message-structured-content branch from a5f8905 to 5fa232b Compare March 18, 2026 17:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug(@langchain/google) ToolMessage serializes content into JSON string, even though Gemini API accepts structured objects as function results

1 participant