Skip to main content
Back to Community & Earn Hub

Handling structured JSON output when switching between OpenAI, Anthropic, and local vLLM behind one router

@ananya_iyer2 hours ago 1749 views

We have a unified gateway (9routerx / OpenAI-compatible proxy) that routes requests across hosted frontier APIs and our internal vLLM cluster.

Regular chat completions swap cleanly, but strict structured JSON outputs (response_format / Pydantic schemas) keep tripping us up whenever a request fails over or switches providers:

  1. One provider rejects default or title fields inside strict JSON Schema.
  2. Another requires additionalProperties: false on every nested object.
  3. Local vLLM (xgrammar) handles simple schemas fast, but chokes or stalls on deeply nested anyOf unions generated by Pydantic v2.

How are teams normalizing Pydantic schemas in one place so structured outputs work predictably across every backend?

1 Answers

Community Answers

@elena_rostova Accepted Answer2 hours ago

Two rules keep structured outputs portable across OpenAI, Anthropic, and vLLM without provider-specific headaches:

  1. Write a tiny schema sanitizer on Pydantic's model_json_schema(): Recursively strip title, default, and examples, and force additionalProperties: False on every "type": "object" node before sending it to the router.
  2. Avoid Optional[Union[A, B]] / deep anyOf in tool/output schemas: Keep fields flat (kind: Literal["a", "b"] + flat nullable fields) so xgrammar / constrained decoding compiles the FSM in < 5ms on every backend.

If a fallback provider doesn't support strict grammar mode at all, fall back to a single-turn pydantic.model_validate_json() retry loop with the validation error message.

Have a solution or working configuration?

Sign in or create a free account to answer this question (+10 pts, +25 pts if accepted) and tag helpful AIStoreHub repositories.