Repository navigation
fix(llm/schema): flatten $ref behind a model field named description - #5604
aniketwaghh wants to merge 1 commit into
Conversation
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.
There was a problem hiding this comment.
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'} |
There was a problem hiding this comment.
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>
| 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'} |
|
Closing in favour of #5605 - same approach, and cleaner for also dropping the I have put the extra cases on that PR: |
Closes #5603
What
SchemaOptimizer.create_optimized_json_schemawalks the schema dispatching on JSON Schema keyword names, and it walks intopropertiesmaps too, where the keys are the user's field names rather than keywords. A field nameddescriptiontherefore hits theelif key == 'description':branch, which copies the value straight through without recursing, so its$refis never inlined.$defshas already been stripped by then, and the schema we send the provider points at nothing.#2324 fixed exactly this for a field named
titleby threading anin_propertiesflag through the walker. This does the same for the rest of the keyword branches: once we know we are inside apropertiesmap, every key is a field name, so recurse on the value and skip the keyword handling entirely.Before, on d2fcda5:
After:
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 carriesand that dict is what
ChatOpenAIputs inresponse_formatwithstrict: True, whatChatAnthropicputs ininput_schema, and so on.The change also covers a field named
properties, which used to keep its metadatatitlebecausein_properties=Trueleaked into the field's own schema. Every other keyword branch behaves identically before and after: for keys liketype,items,required,minimumthe old path already ended up callingoptimize_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$refsurvives, and the nested object is fully inlined withadditionalProperties: falsetest_structured_done_action_has_no_dangling_ref- same assertion through the publicTools(output_model=...)pathtest_optimizer_keeps_fields_named_after_schema_keywords- fields namedtitle,description,type,properties,items,requiredall survive, and their property schemas are still stripped of metadata titlesAll three fail on main:
and pass with it:
Full suite, macOS arm64:
2 failed, 1106 passed, 34 skipped2 failed, 1103 passed, 34 skippedSame two failures either way - the order-dependent
test_beta_agent.pypair from #5425, which pass in isolation.browser_use/llm/tests:5 failed, 19 passed, 15 skippedon both, the 5 being the live-APItest_single_stepcases 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 statusafterwards shows only the two intended files.Summary by cubic
Fixes the schema optimizer so model fields named after JSON Schema keywords (like
descriptionorproperties) no longer leave dangling$refs in the optimized schema. Previously such a field was copied through verbatim, keeping a$refthat pointed at$defsafter it was stripped, which made provider schemas invalid; now the walker recurses into every value inside apropertiesmap and flattens the$ref, and fields namedpropertiesno longer keep their metadatatitle. Closes #5603.Written for commit 8acb656. Summary will update on new commits.