Skip to content

fix(llm/google): decode structured-output fields Gemini returns as JSON strings - #5915

Open
HardMax71 wants to merge 5 commits into
browser-use:mainfrom
HardMax71:fix/chatgoogle-repair-serialized-fields
Open

HardMax71 wants to merge 5 commits into
browser-use:mainfrom
HardMax71:fix/chatgoogle-repair-serialized-fields

Conversation

@HardMax71

@HardMax71 HardMax71 commented Sep 25, 2026 •

Copy link
Copy Markdown

Fixes #5914

Gemini sometimes returns a list field of the structured output, the AgentOutput action list typically, JSON-encoded as a string. ChatGoogle validated it as is, so the call failed with Input should be a valid list and the agent retried the step. ChatGoogle now validates through a small _validate_output: on a ValidationError it decodes the top-level fields that validation rejected, when they are strings starting with [ or {, with the same control-character fallback ChatAnthropic._repair_serialized_fields uses, and validates again. Fields that validated are left alone, so a string field that happens to hold JSON (a bracket-leading thinking) stays a string. It covers all three structured-output branches (native JSON with response.parsed, native JSON parsed from text, and the fallback JSON mode). A payload that validates the first time is untouched, and a value that still does not decode fails as before.

ChatAnthropic and the Bedrock client each carry the same loop, which decodes every bracket-leading field, not only the rejected ones. I kept this change to ChatGoogle since several open PRs touch those files. Happy to move the three into one shared helper, with this narrower rule, in a follow-up if you want that.

Testing:

  • tests/ci/test_google_serialized_structured_output.py (new, offline, stubbed client returning real GenerateContentResponse objects): a stringified action is decoded in each of the three branches, including one with a raw newline inside the JSON string; an object field arriving as a string is decoded; a valid bracket-leading string field next to a stringified action is kept as a string; an undecodable string still raises ModelProviderError. The six decode cases fail on main.
  • tests/ci/models/test_llm_google.py, tests/ci/test_llm_retries.py, tests/ci/test_anthropic_serialized_tool_input.py, browser_use/llm/tests/test_gemini_image.py plus the new file: all pass, 1 skipped (needs GOOGLE_API_KEY).
  • pre-commit run on the changed files: all hooks pass. Merges clean with fix(llm/google): retry SDK-raised errors with retryable status codes #5651, the other open PR on llm/google/chat.py.
  • The repro in Bug: ChatGoogle does not repair an action list Gemini returns as a JSON string, so the step fails validation and retries #5914 against this branch: parsed OK, action type: list, requests: 1.

…ON strings

Gemini sometimes returns a list field of the structured output, the AgentOutput action list typically, JSON-encoded as a string. ChatGoogle validated it as is, so the call failed with "Input should be a valid list" and the agent retried the step. On a ValidationError, ChatGoogle now decodes string fields that start with "[" or "{" (with the same control-character fallback as ChatAnthropic's _repair_serialized_fields) and validates again, in the native JSON, text and fallback JSON branches.

Fixes browser-use#5914
Copilot AI lite review requested due to automatic review settings September 25, 2026 18:13

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 issues found across 2 files

Heads up: you’ve reached your flex budget. Increase your flex budget or wait for usage to reset.

Fix all with cubic | Re-trigger cubic

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="tests/ci/test_google_serialized_structured_output.py">

<violation number="1" location="tests/ci/test_google_serialized_structured_output.py:53">
P3: Every test only covers string fields that start with `[`, but `_validate_output` also decodes `{`-prefixed dict fields. Add a case where a dict-typed field (e.g., a nested object) arrives JSON-encoded as a string, so a regression in the dict decode path can't slip through.</violation>
</file>

<file name="browser_use/llm/google/chat.py">

<violation number="1" location="browser_use/llm/google/chat.py:251">
P3: `_validate_output` runs on any `ValidationError` and decodes every top-level string that starts with `[` or `{`, not just the fields that actually failed validation. A legitimately string-valued field (e.g., `thinking`) that happens to be bracket-leading and valid JSON gets decoded into a list/object when the real failure is elsewhere; the second `ValidationError` then reports the wrong field, masking the actual problem. Scope the repair to the failed field locations from the first `ValidationError` (`e.errors()` + `e.error_count()` locations, or decode only fields the schema declares as non-string).</violation>
</file>

Comment thread browser_use/llm/google/chat.py Resolved
The repair decoded every top-level string starting with [ or {, so a valid string field that happens to hold JSON (a bracket-leading thinking, say) was turned into a list or object and failed the second validation, even when the stringified action alone was the problem. It now decodes only the top-level fields named in the first ValidationError's locations. Tests cover that case and an object field arriving as a string.

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 2 files (changes from recent commits).

Heads up: you’ve reached your flex budget. Increase your flex budget or wait for usage to reset.

Fix all with cubic | Re-trigger cubic

Covers the case the rejected-key set is built for: two fields JSON-encoded as strings at once.
@mintlify

mintlify Bot commented Oct 21, 2026 •

Copy link
Copy Markdown

Preview approval required

This pull request comes from a fork, so Mintlify won't build a preview until someone with write access approves commit 6292972.

Approve preview

@HardMax71

Copy link
Copy Markdown
Author

@gregpr07 could you review this when you get a chance? ChatGoogle decodes the structured-output fields Gemini returns as JSON strings. It's up to date with main, CI is green and the bot reviews are handled.

sun04222 added a commit to sun04222/browser-use that referenced this pull request Oct 23, 2026

This branch has not been deployed

No deployments
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: ChatGoogle does not repair an action list Gemini returns as a JSON string, so the step fails validation and retries

2 participants