Skip to content

Bug: SchemaOptimizer skips strict-mode required for a nested model under a field named "required" #6209

Open

Description

@mengchen-atx

Browser Use Version

0.13.13 (main @ 3df5806)

Bug Description, Steps to Reproduce, Screenshots

I already have a fix with a regression test and will open the PR myself.

SchemaOptimizer.create_optimized_json_schema (used for OpenAI/Azure strict structured output) does not make an object strict when it sits under a model field literally named required. Its optional properties are left out of that object's required array, so the schema is not valid for OpenAI strict mode, which needs every key in properties listed in required. Any output model (or action param model) with such a field fails on every call.

Steps:

  1. Define a nested model with an optional field and use it under a field called required (see code below).
  2. Call SchemaOptimizer.create_optimized_json_schema(Form).

Actual output:

optional: ['name', 'note']
required: ['name']

Expected: both say ['name', 'note'], since the two fields have the same type and only the name differs.

Root cause: browser_use/llm/schema.py:197, in _make_strict_compatible:

for key, value in schema.items():
    if isinstance(value, (dict, list)) and key != 'required':
        SchemaOptimizer._make_strict_compatible(value)

The key != 'required' check is meant to skip the required keyword, but the same loop also walks the properties dict, where keys are field names. So the field named required is never visited. The skip isn't needed anyway: the required keyword is a list of strings, and recursing into it does nothing. This is the same kind of problem as #5603 / #5829 (property names treated as schema keywords), but in the strict-mode pass, which none of the open PRs touch.

Fix: drop the key check and recurse into every dict/list value. I'll add a regression test to tests/ci/models/test_llm_schema_optimizer.py. I'll open the PR myself.

Failing Python Code

from pydantic import BaseModel
from browser_use.llm.schema import SchemaOptimizer

class Inner(BaseModel):
    name: str
    note: str | None = None

class Form(BaseModel):
    optional: Inner
    required: Inner  # same type, only the field name differs

schema = SchemaOptimizer.create_optimized_json_schema(Form)
print("optional:", schema["properties"]["optional"]["required"])
print("required:", schema["properties"]["required"]["required"])

LLM Model

Any model that uses strict structured output via SchemaOptimizer (e.g. ChatOpenAI / ChatAzureOpenAI). The repro doesn't call a model.

Operating System & Browser Versions

Ubuntu (Linux), Python 3.12, pydantic 2.x. No browser needed.

Full DEBUG Log Output

$ python repro.py
optional: ['name', 'note']
required: ['name']

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions