Repository navigation
fix(tools): support JSON Schema type arrays (e.g. ["string", "null"]) in schema_dict_to_pydantic_model - #5919
liwenjie200543 wants to merge 2 commits into
Conversation
…c_model Fixes browser-use#5904 LLMs routinely emit the standard JSON Schema nullable-union form `"type": ["string", "null"]`. _resolve_type() only handled a scalar `type`, so the list fell through to _PRIMITIVE_MAP.get(list, str) and raised TypeError: unhashable type: 'list'. service.py then silently swallowed the failure and the run lost its output_schema entirely. Changes: - _resolve_type(): resolve each member of a type array and join them into a union ("null" contributes NoneType); an empty/all-null array resolves to NoneType. - _build_model(): normalize type arrays when selecting field defaults so the nullable case behaves like the proprietary nullable flag. Added four regression tests to TestSchemaDictToPydanticModel covering nullable strings, single-element arrays, nullable nested objects and multi-type unions. TestSchemaDictToPydanticModel passes 23/23.
There was a problem hiding this comment.
All reported issues were addressed across 2 files
Heads up: you’ve reached your flex budget. Increase your flex budget or wait for usage to reset.
Re-trigger cubic
… unions
Address cubic review findings:
- Move the type-array branch in _resolve_type before the enum check so
{"type": ["string", "null"], "enum": [...]} keeps its nullability
(str | None) instead of short-circuiting to plain str.
- Extract _split_type_array() so _resolve_type and _build_model share one
interpretation of type arrays + the nullable flag (they could previously
diverge on edge cases like an empty type array).
- Non-required multi-type unions (e.g. ["string", "integer"]) now default
to None (with | None) instead of a member-order-dependent zero value,
matching the enum/nested-object fallback semantics.
- Tests: pin ['null']-only arrays, the omitted-field nullable default,
enum+nullable type arrays, and union order-independence.
|
Addressed all five cubic findings in d6e8dc8:
|
|
👋 This PR has been automatically marked as stale because it hasn't had activity for 45 days. To keep this PR open:
This will be automatically closed in 14 days if no further activity occurs. Thanks for contributing to browser-use! 🤖 |
|
🔒 This PR was automatically closed because it was stale for 14 days with no activity. Don't worry! If you'd like to continue this work:
Thanks for contributing to browser-use! 🤖 |
Summary
Fixes #5904.
LLMs routinely emit the standard JSON Schema nullable-union form
"type": ["string", "null"]._resolve_type()only handled a scalartype, so the list fell through to_PRIMITIVE_MAP.get(list, str)and raisedTypeError: unhashable type: 'list'.service.pythen silently swallowed the failure, and the run lost itsoutput_schemaentirely (structured output silently degraded to plain text).Changes
_resolve_type(): resolve each member of a type array and join them into a union ("null"contributesNoneType); an empty/all-null array resolves toNoneType. Nested schemas (e.g.{"type": ["object", "null"], "properties": {...}}) recurse correctly._build_model(): normalize type arrays when selecting field defaults, so the nullable case behaves like the proprietarynullableflag instead of choking on the list.Testing
Added four regression tests to the existing
TestSchemaDictToPydanticModelclass intests/ci/test_structured_extraction.py:["string", "null"]) behaves likenullable: True["string"]) resolves to plainstr["object", "null"]with properties)["string", "integer"])pytest tests/ci/test_structured_extraction.py::TestSchemaDictToPydanticModelpasses 23/23.Note: this PR was prepared with AI assistance (human-directed and human-reviewed).
Summary by cubic
Fixes #5904 so JSON Schema type arrays like
["string", "null"]no longer crash structured output generation. Previously_resolve_type()only handled a scalartype, so arrays raised aTypeErrorthatservice.pysilently swallowed, dropping theoutput_schemaand degrading structured output to plain text._resolve_type()now resolves each member of a type array into a union, with"null"contributingNoneType; the branch runs before the enum check so enum + nullable arrays keep nullability._split_type_array()normalization lets_build_model()treat nullable arrays like thenullableflag, and multi-type unions now default toNoneinstead of a member-order-dependent zero value.Written for commit d6e8dc8. Summary will update on new commits.