Skip to content

fix(llm/schema): flatten $ref behind a model field named description - #5604

Closed
aniketwaghh wants to merge 1 commit into
browser-use:mainfrom
aniketwaghh:fix_schema_optimizer_property_name_collision
Closed

aniketwaghh wants to merge 1 commit into
browser-use:mainfrom
aniketwaghh:fix_schema_optimizer_property_name_collision

Conversation

@aniketwaghh

@aniketwaghh aniketwaghh commented Aug 30, 2026 •

Copy link
Copy Markdown
Contributor

Closes #5603

What

SchemaOptimizer.create_optimized_json_schema walks the schema dispatching on JSON Schema keyword names, and it walks into properties maps too, where the keys are the user's field names rather than keywords. A field named description therefore hits the elif key == 'description': branch, which copies the value straight through without recursing, so its $ref is never inlined. $defs has already been stripped by then, and the schema we send the provider points at nothing.

#2324 fixed exactly this for a field named title by threading an in_properties flag through the walker. This does the same for the rest of the keyword branches: once we know we are inside a properties map, every key is a field name, so recurse on the value and skip the keyword handling entirely.

Before, on d2fcda5:

>>> SchemaOptimizer.create_optimized_json_schema(Article)   # class Article: headline: str; description: Details
{'properties': {'headline': {'type': 'string'},
                'description': {'$ref': '#/$defs/Details'}},
 'required': ['headline', 'description'],
 'type': 'object',
 'additionalProperties': False}

>>> jsonschema.validate({'headline': 'x', 'description': {'summary': 'y'}}, schema)
_WrappedReferencingError: PointerToNowhere: '/$defs/Details' does not exist within {...}

After:

{'properties': {'headline': {'type': 'string'},
                'description': {'properties': {'summary': {'type': 'string'}},
                                'required': ['summary'],
                                'type': 'object',
                                'additionalProperties': False}},
 'required': ['headline', 'description'],
 'type': 'object',
 'additionalProperties': False}

Same through the agent path (Tools(output_model=Article) -> AgentOutput.type_with_custom_actions), which is where it actually bites: on main the done action carries

/properties/action/items/anyOf[0]/properties/done/properties/data/properties/description/$ref -> #/$defs/Details

and that dict is what ChatOpenAI puts in response_format with strict: True, what ChatAnthropic puts in input_schema, and so on.

The change also covers a field named properties, which used to keep its metadata title because in_properties=True leaked into the field's own schema. Every other keyword branch behaves identically before and after: for keys like type, items, required, minimum the old path already ended up calling optimize_schema(value, defs_lookup), which is what the new branch does.

Tests

Three tests added to tests/ci/models/test_llm_schema_optimizer.py, the file that came out of #2324:

  • test_optimizer_flattens_ref_behind_field_named_description - no $ref survives, and the nested object is fully inlined with additionalProperties: false
  • test_structured_done_action_has_no_dangling_ref - same assertion through the public Tools(output_model=...) path
  • test_optimizer_keeps_fields_named_after_schema_keywords - fields named title, description, type, properties, items, required all survive, and their property schemas are still stripped of metadata titles

All three fail on main:

$ git stash push -- browser_use/llm/schema.py
$ .venv/bin/python -m pytest tests/ci/models/test_llm_schema_optimizer.py -q -o addopts=""
E    AssertionError: Unresolved $ref left in schema: ['/properties/description/$ref -> #/$defs/Details']
E    AssertionError: Unresolved $ref left in schema: ['/properties/action/items/anyOf[0]/properties/done/properties/data/properties/description/$ref -> #/$defs/Details']
E    AssertionError: assert {'title': 'De...pe': 'string'} == {'type': 'string'}
3 failed, 2 passed in 0.06s

and pass with it:

$ .venv/bin/python -m pytest tests/ci/models/test_llm_schema_optimizer.py -q -o addopts=""
5 passed in 0.03s

Full suite, macOS arm64:

  • branch: 2 failed, 1106 passed, 34 skipped
  • clean main at d2fcda5: 2 failed, 1103 passed, 34 skipped

Same two failures either way - the order-dependent test_beta_agent.py pair from #5425, which pass in isolation.

browser_use/llm/tests: 5 failed, 19 passed, 15 skipped on both, the 5 being the live-API test_single_step cases with no keys set.

pre-commit run --files browser_use/llm/schema.py tests/ci/models/test_llm_schema_optimizer.py: every hook passed, including ruff v0.12.10 and pyright v1.1.404 as pinned in .pre-commit-config.yaml. git status afterwards shows only the two intended files.


Summary by cubic

Fixes the schema optimizer so model fields named after JSON Schema keywords (like description or properties) no longer leave dangling $refs in the optimized schema. Previously such a field was copied through verbatim, keeping a $ref that pointed at $defs after it was stripped, which made provider schemas invalid; now the walker recurses into every value inside a properties map and flattens the $ref, and fields named properties no longer keep their metadata title. Closes #5603.

Written for commit 8acb656. Summary will update on new commits.

Review in cubic

Property names inside a properties map were being matched against JSON Schema
keyword names, so a field called description took the description branch and was
copied through verbatim, leaving a $ref that points at the stripped $defs.

@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.

1 issue found across 2 files

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/models/test_llm_schema_optimizer.py">

<violation number="1" location="tests/ci/models/test_llm_schema_optimizer.py:151">
P3: This test only validates the resulting schema values for two of the six keyword-named fields (description, properties). The set-membership assertion at line 149 proves the other four (title, type, items, required) weren't dropped, but not that their schemas are intact — e.g. a regression that mangled the `items` or `type` property value would still pass. Assert each keyword field's schema (all should equal {'type': 'string'}) so the test actually pins the behavior it claims to verify.</violation>
</file>

Reply with feedback, questions, or to request a fix.

Fix all with cubic | Re-trigger cubic


assert set(schema['properties']) == set(KeywordFields.model_fields)
# metadata titles are still dropped from the property schemas themselves
assert schema['properties']['description'] == {'type': 'string'}

@cubic-dev-ai cubic-dev-ai Bot Aug 30, 2026 •

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.

P3: This test only validates the resulting schema values for two of the six keyword-named fields (description, properties). The set-membership assertion at line 149 proves the other four (title, type, items, required) weren't dropped, but not that their schemas are intact — e.g. a regression that mangled the items or type property value would still pass. Assert each keyword field's schema (all should equal {'type': 'string'}) so the test actually pins the behavior it claims to verify.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At tests/ci/models/test_llm_schema_optimizer.py, line 151:

<comment>This test only validates the resulting schema values for two of the six keyword-named fields (description, properties). The set-membership assertion at line 149 proves the other four (title, type, items, required) weren't dropped, but not that their schemas are intact — e.g. a regression that mangled the `items` or `type` property value would still pass. Assert each keyword field's schema (all should equal {'type': 'string'}) so the test actually pins the behavior it claims to verify.</comment>

<file context>
@@ -74,3 +76,77 @@ def test_gemini_schema_retains_required_fields():
+
+	assert set(schema['properties']) == set(KeywordFields.model_fields)
+	# metadata titles are still dropped from the property schemas themselves
+	assert schema['properties']['description'] == {'type': 'string'}
+	assert schema['properties']['properties'] == {'type': 'string'}
</file context>
Suggested change
assert schema['properties']['description'] == {'type': 'string'}
assert schema['properties']['title'] == {'type': 'string'}
assert schema['properties']['description'] == {'type': 'string'}
assert schema['properties']['type'] == {'type': 'string'}
assert schema['properties']['properties'] == {'type': 'string'}
assert schema['properties']['items'] == {'type': 'string'}
assert schema['properties']['required'] == {'type': 'string'}
Fix with cubic

@aniketwaghh

Copy link
Copy Markdown
Contributor Author

Closing in favour of #5605 - same approach, and cleaner for also dropping the and not in_properties that I left behind on the title branch.

I have put the extra cases on that PR: additionalProperties and $defs as field names are dropped from the schema entirely on d2fcda5, not just left with a dangling $ref. #5605 fixes those too.

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: SchemaOptimizer leaves a dangling $ref when a model field is named "description"

1 participant