Skip to content

Fix: don’t drop real title fields when optimising schema - #2324

Merged
mertunsall merged 6 commits into
browser-use:mainfrom
gobii-ai:2025-07-06-fix-schema-optimizer
Jul 7, 2025
Merged

mertunsall merged 6 commits into
browser-use:mainfrom
gobii-ai:2025-07-06-fix-schema-optimizer

Conversation

@ai-christianson

@ai-christianson ai-christianson commented Jul 6, 2025 •

Copy link
Copy Markdown
Contributor

What happened?

When SchemaOptimizer cleans up a Pydantic JSON-Schema it throws away every
"title" key, assuming it’s just the auto-generated object label.
That’s fine—until you have a real model field also called title.
In that case the field silently disappears from the optimised schema, breaking
strict-mode validation downstream (see failing test in this patch).

tl;dr without this patch, an output schema with a title field gets broken by the schema optimizer.

The fix (tiny but robust)

  • Track whether the optimiser is currently iterating over a properties map.
  • Skip "title" only when we’re not inside properties
    (i.e. only for metadata titles).
  • Net code change: ~5 logical lines; no API surface touched.
# before
if key == "title":
    continue

# after
if key == "title" and not in_properties:
    continue

Why this is safe to merge

  • Works for any model that has a field named title—no project-specific assumptions.
  • No side-effects: $ref flattening, anyOf handling, and strict-mode logic are untouched.
  • Added regression test fails on main, passes with this patch:
class Foo(BaseModel):
    title: str
    value: int

Summary by cubic

Fixed a bug where the schema optimizer removed real model fields named title, causing them to disappear from the output schema.

  • Bug Fixes
    • Now only skips metadata title keys, not actual fields inside properties.
    • Added a regression test to ensure all fields are preserved.

@CLAassistant

CLAassistant commented Jul 6, 2025 •

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.

cubic reviewed 2 files and found no issues. Review PR in cubic.dev.

@ai-christianson
ai-christianson force-pushed the 2025-07-06-fix-schema-optimizer branch from 8feb520 to 8430f21 Compare July 6, 2025 21:28
@ai-christianson
ai-christianson marked this pull request as draft July 6, 2025 21:39
@ai-christianson
ai-christianson marked this pull request as ready for review July 6, 2025 21:49

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

cubic reviewed 3 files and found no issues. Review PR in cubic.dev.

Comment thread browser_use/agent/system_prompt.md Resolved
@mertunsall

Copy link
Copy Markdown
Contributor

Great catch! Thanks a lot for this PR!

@mertunsall
mertunsall merged commit 0e858c7 into browser-use:main Jul 7, 2025

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.

this should probably be in tests/ci/ not in controller/tests

soyun11 pushed a commit to soyun11/browser-use that referenced this pull request May 15, 2026
…se#2324)

### What happened?

When `SchemaOptimizer` cleans up a Pydantic JSON-Schema it throws away
every
`"title"` key, assuming it’s just the auto-generated object label.  
That’s fine—until you have a real model field *also* called `title`.  
In that case the field silently disappears from the optimised schema,
breaking
strict-mode validation downstream (see failing test in this patch).

**tl;dr** without this patch, an output schema with a `title` field gets
broken by the schema optimizer.

### The fix (tiny but robust)

* Track whether the optimiser is currently iterating over a `properties`
map.
* Skip `"title"` **only** when we’re *not* inside `properties`
  (i.e. only for metadata titles).
* Net code change: ~5 logical lines; no API surface touched.

```python
# before
if key == "title":
    continue

# after
if key == "title" and not in_properties:
    continue
```

### Why this is safe to merge

* Works for *any* model that has a field named `title`—no
project-specific assumptions.
* No side-effects: `$ref` flattening, `anyOf` handling, and strict-mode
logic are untouched.
* Added regression test fails on `main`, passes with this patch:

```python
class Foo(BaseModel):
    title: str
    value: int
```

    
<!-- This is an auto-generated description by cubic. -->
---

## Summary by cubic
Fixed a bug where the schema optimizer removed real model fields named
`title`, causing them to disappear from the output schema.

- **Bug Fixes**
- Now only skips metadata `title` keys, not actual fields inside
`properties`.
  - Added a regression test to ensure all fields are preserved.

<!-- End of auto-generated description by cubic. -->
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.

4 participants