AIToday
Large Language ModelsAI Coding AssistantsZenn AI/MLPublished: Dec 5, 2026, 22:01 JST

Zenn tutorial: pydantic and zod fallback design for reliable LLM JSON

Send on LINE
Zenn tutorial: pydantic and zod fallback design for reliable LLM JSON

3 Key Points

  1. What happened

    A Zenn article by developer @aman179102 lays out a pipeline that validates LLM output against a pydantic or zod schema, respects the Retry-After header for exponential backoff, and falls back deterministically — recording failed providers and starting from a different one next time, skipping all providers once failures pass a threshold.

  2. Why it matters

    The post targets the practical gaps teams hit in production — models returning malformed JSON, API key rate limits stopping service even with multiple providers, and one provider's failure not triggering an automatic switch — so the design gives teams running AI data generation a concrete pattern to reduce those failures to a defined error.

Ask AI

Ask the AI about this article. Questions and answers are published on this page

Context & Analysis

The tutorial's framing is that LLMs have become essential for data generation in many business workflows, but production use runs into a recurring set of failure modes: the model does not return JSON as expected or emits a syntax error, API key rate limits stop service even when multiple providers are available, one provider's failure does not automatically switch to another, and JSON that fails the schema breaks downstream processing. The proposed architecture addresses those by layering three mechanisms: schema validation with pydantic or zod, rate-limit backoff that respects the Retry-After header, and a deterministic fallback order across a provider list that includes OpenAI GPT-4o, Anthropic Claude and Azure OpenAI. The Python sample shows the call and validation loop, including a WeatherResponse schema and a response_format setting of json_object, while a TypeScript version of the fallback logic is aimed at front-end use. The author points readers to GitHub and a portfolio for further work.

FAQ
What happens when one provider keeps failing?
The deterministic fallback records the failed provider and starts from a different one next time. If failures pass a threshold, it skips all providers and returns an error.
How does the code handle rate limits?
It honors the Retry-After header and retries with exponential backoff. The Python sample sets MAX_RETRIES to 5 and a BACKOFF_FACTOR of 2.0.
What schema tools does the tutorial use for validation?
It uses pydantic in Python and zod in TypeScript to define the expected JSON structure. The Python sample defines a WeatherResponse model with city, temperature and condition fields.
Send on LINE

AI news that matters for your work, delivered every morning.

Pick your industry and the AI tools you use, and get news related to your work every day.

By signing up you agree to the Terms of Service and Privacy Policy.

Free · 30 seconds with Google · unsubscribe anytimeWhat is AIToday? →

Related Articles

Next articlePlaud Note Pro Flow Edition gets 20% off in first Black Friday sale