Commit a98801a
authored
Fix schema parsing bug for Pydantic
# why
JSON schema parsing for schemas generated from Pydantic
`.model_json_schema()` were dropping the nested references
# what changed
Before (from test script) with schema:
```python
class RequiredInputType(StrEnum):
USERNAME = "username"
EMAIL = "email"
PHONE = "phone"
OTP = "otp"
PASSWORD = "password"
CARD_LAST_8 = "card_last_8"
SOMETHING_ELSE = "something_else"
class PageState(StrEnum):
LOGIN_FORM = "login_form"
ERROR = "error"
AUTHENTICATED = "authenticated"
UNKNOWN = "unknown"
class PageAnalysis(BaseModel):
page_state: PageState
required_input: RequiredInputType | None = None
contact_id_of_required_input: str | None = None
error_message: str | None = None
reasoning: str = ""
```
<img width="654" height="373" alt="Screenshot 2026-03-23 at 1 09 01 PM"
src="https://github.com/user-attachments/assets/29a24eea-cfda-40a8-8e43-10f959c71ad2"
/>
After:
<img width="650" height="310" alt="Screenshot 2026-03-23 at 1 06 17 PM"
src="https://github.com/user-attachments/assets/9f62d1ad-04bb-41e4-9dbc-85d7b68c84aa"
/>
1. Added resolveRefs() function that inlines $ref/$defs before Zod
conversion (Pydantic
v2's model_json_schema() generates these)
2. Changed z.string().refine() → z.enum() so enum constraints are
expressed in the
schema sent to the LLM, not just validated post-hoc
# test plan
<!-- This is an auto-generated description by cubic. -->
---
## Summary by cubic
Fixes missing nested `$ref` when converting Pydantic v2
`.model_json_schema()` to Zod in `@browserbasehq/stagehand-server-v3`,
and emits proper `z.enum()` types so LLM-facing schemas are accurate.
Adds a `test:unit` script, moves tests to `tests/`, and updates the
runner to scan directories or single files.
- **Bug Fixes**
- Resolves `$ref` from `$defs` before conversion via `resolveRefs()`
with a cycle guard, handling refs in objects, arrays,
`anyOf`/`oneOf`/`allOf`, and root-level refs (unknown refs fall back
safely).
- Replaces `z.string().refine()` with `z.enum()` so enum values are
surfaced in generated schemas and enforced.
<sup>Written for commit ae9e2e5.
Summary will update on new commits. <a
href="https://cubic.dev/pr/browserbase/stagehand/pull/1873">Review in
cubic</a></sup>
<!-- End of auto-generated description by cubic. -->.model_json_schema() (browserbase#1873)1 parent f3fe7ce commit a98801a
17 files changed
Lines changed: 890 additions & 12 deletions
File tree
- .changeset
- packages/server-v3
- scripts
- src/lib
- tests
- integration
- v3
- unit
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
| 20 | + | |
20 | 21 | | |
21 | 22 | | |
22 | 23 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
93 | 93 | | |
94 | 94 | | |
95 | 95 | | |
96 | | - | |
| 96 | + | |
97 | 97 | | |
98 | 98 | | |
99 | 99 | | |
| |||
224 | 224 | | |
225 | 225 | | |
226 | 226 | | |
227 | | - | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
228 | 233 | | |
229 | 234 | | |
230 | 235 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
21 | 77 | | |
22 | 78 | | |
23 | 79 | | |
| |||
26 | 82 | | |
27 | 83 | | |
28 | 84 | | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
29 | 94 | | |
30 | 95 | | |
31 | 96 | | |
32 | | - | |
| 97 | + | |
33 | 98 | | |
34 | 99 | | |
35 | 100 | | |
| |||
45 | 110 | | |
46 | 111 | | |
47 | 112 | | |
48 | | - | |
| 113 | + | |
49 | 114 | | |
50 | 115 | | |
51 | 116 | | |
| |||
59 | 124 | | |
60 | 125 | | |
61 | 126 | | |
62 | | - | |
| 127 | + | |
63 | 128 | | |
64 | 129 | | |
65 | 130 | | |
| |||
84 | 149 | | |
85 | 150 | | |
86 | 151 | | |
87 | | - | |
| 152 | + | |
88 | 153 | | |
89 | 154 | | |
90 | 155 | | |
| |||
109 | 174 | | |
110 | 175 | | |
111 | 176 | | |
112 | | - | |
| 177 | + | |
113 | 178 | | |
114 | 179 | | |
115 | 180 | | |
| |||
118 | 183 | | |
119 | 184 | | |
120 | 185 | | |
121 | | - | |
122 | | - | |
| 186 | + | |
| 187 | + | |
123 | 188 | | |
124 | 189 | | |
125 | 190 | | |
| |||
File renamed without changes.
File renamed without changes.
File renamed without changes.
0 commit comments