Skip to content

fix(tools): support JSON Schema type arrays (e.g. ["string", "null"]) in schema_dict_to_pydantic_model - #5919

Closed
liwenjie200543 wants to merge 2 commits into
browser-use:mainfrom
liwenjie200543:fix/schema-type-arrays
Closed

liwenjie200543 wants to merge 2 commits into
browser-use:mainfrom
liwenjie200543:fix/schema-type-arrays

Conversation

@liwenjie200543

@liwenjie200543 liwenjie200543 commented Sep 26, 2026 •

Copy link
Copy Markdown

Summary

Fixes #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 (structured output silently degraded to plain text).

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. 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 proprietary nullable flag instead of choking on the list.

Testing

Added four regression tests to the existing TestSchemaDictToPydanticModel class in tests/ci/test_structured_extraction.py:

  • nullable string via type array (["string", "null"]) behaves like nullable: True
  • single-element array (["string"]) resolves to plain str
  • nullable nested object (["object", "null"] with properties)
  • multi-type union (["string", "integer"])

pytest tests/ci/test_structured_extraction.py::TestSchemaDictToPydanticModel passes 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 scalar type, so arrays raised a TypeError that service.py silently swallowed, dropping the output_schema and degrading structured output to plain text.

  • _resolve_type() now resolves each member of a type array into a union, with "null" contributing NoneType; the branch runs before the enum check so enum + nullable arrays keep nullability.
  • Shared _split_type_array() normalization lets _build_model() treat nullable arrays like the nullable flag, and multi-type unions now default to None instead of a member-order-dependent zero value.
  • Added regression tests for nullable strings, null-only arrays, enum+nullable unions, single-element arrays, nullable nested objects, and multi-type unions.

Written for commit d6e8dc8. Summary will update on new commits.

Review in cubic

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

CLAassistant commented Sep 26, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@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

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

Re-trigger cubic

Comment thread tests/ci/test_structured_extraction.py Resolved
Comment thread tests/ci/test_structured_extraction.py Resolved
… 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.
@liwenjie200543

Copy link
Copy Markdown
Author

Addressed all five cubic findings in d6e8dc8:

  • P2 (enum + nullable type array): the type-array branch in _resolve_type now runs before the enum check, so {"type": ["string", "null"], "enum": ["low", "high"]} resolves to str | None instead of silently dropping nullability. Pinned by test_type_array_with_enum_stays_nullable.
  • P3 (duplicated normalization): extracted _split_type_array() — _resolve_type and _build_model now share one interpretation of type arrays and the nullable flag, so edge cases like an empty type array can no longer diverge.
  • P3 (member-order-dependent union defaults): non-required multi-type unions now default to None (annotation widened with | None), matching the enum/nested-object fallback. test_type_array_multi_type_union now asserts order-independence between ["string", "integer"] and ["integer", "string"].
  • P3 tests: added test_type_array_null_only (pins the ['null']-only branch) and an omitted-field assertion (Model().nickname is None) for the default-normalization half of the fix.

tests/ci/test_structured_extraction.py passes 36/36 locally. The test_tools CI failure is test_search_action — a live DuckDuckGo search timing out after 30s in CI, unrelated to this change (no shared code path with schema_utils).

@github-actions

Copy link
Copy Markdown

👋 This PR has been automatically marked as stale because it hasn't had activity for 45 days.

To keep this PR open:

  • Rebase against the latest main branch
  • Address any review feedback or merge conflicts
  • Add a comment explaining the current status
  • Add the work-in-progress label if you're still actively working on this

This will be automatically closed in 14 days if no further activity occurs.

Thanks for contributing to browser-use! 🤖

@github-actions github-actions Bot added the stale label Nov 11, 2026
@github-actions

Copy link
Copy Markdown

🔒 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:

  • Reopen this PR and rebase against main
  • Create a fresh PR with updated changes
  • Join our Discord if you need help

Thanks for contributing to browser-use! 🤖

@github-actions github-actions Bot closed this Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

schema_dict_to_pydantic_model TypeError on JSON Schema type arrays → extract falls back to free-text

2 participants