Fix schema parsing bug for Pydantic `.model_json_schema()`

Merged
MI
8mo ago
+890-12

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 = ""
Screenshot 2026-03-23 at 1 09 01 PM

After:
Screenshot 2026-03-23 at 1 06 17 PM

  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


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.