Fix schema parsing bug for Pydantic `.model_json_schema()`
Merged
MI
8mo ago+890+890+012345678901234567890123456789-12-12-01234567890123456789
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:
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 = ""
After:
- Added resolveRefs() function that inlines $ref/$defs before Zod conversion (Pydantic
v2's model_json_schema() generates these) - 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
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
$reffrom$defsbefore conversion viaresolveRefs()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()withz.enum()so enum values are surfaced in generated schemas and enforced.
- Resolves