ai-machine-learning

8 LLM Structured Output Libraries Ranked: JSON That Works

Written by Mert Batur
Updated May 12, 2026
17 read
8 LLM Structured Output Libraries Ranked: JSON That Works

Picking the best LLM structured output library shouldn't take a week of research. We've built production systems with most of these tools, and we have strong opinions about which ones are worth your time. This ranked list covers all eight major options, from the obvious no. 1 pick to niche engines you'll only need in specific situations. New to structured outputs? Start with our complete guide to LLM structured outputs first.

Our Rankings at a Glance

RankLibraryLanguageBest ForOur Take
1InstructorPython (+ TS, Go, Ruby)Most Python teamsThe default. Start here.
2Vercel AI SDKTypeScriptTS / Next.js projectsThe Instructor of TypeScript
3BAMLPython, TS, Ruby, Go, RustCross-language teamsBest DSL approach, growing fast
4Pydantic AIPythonAgent pipelinesGreat if you're building agents
5XGrammarC++/Rust (engine)Self-hosted LLMsThe engine under vLLM/SGLang
6OutlinesPythonSelf-hosted prototypingPython-native constrained decoding
7LiteLLMPythonMulti-provider proxyPairs with Instructor beautifully
8MarvinPythonQuick prototypingDead simple, limited scope

Now let's break down exactly why each tool earned its spot.

no. 1: Instructor, The Default Choice

Instructor is the most popular structured output library by a wide margin: 12K+ GitHub stars, 3M+ monthly PyPI downloads, and a massive ecosystem of examples, tutorials, and integrations. It earned the top spot because it does the core job, getting typed, validated data out of LLMs, better and more reliably than anything else.

What's Great

The API is beautifully simple. You decorate an existing provider client (OpenAI, Anthropic, Gemini, Ollama, or any of 15+ others), define a Pydantic model, and call client.chat.completions.create() with response_model=YourModel. That's it. Instructor handles JSON Schema generation, response parsing, and, this is the killer feature, automatic retries with validation error feedback. When the LLM produces invalid output, Instructor sends the validation errors back so the model can fix itself. Most of the time, it gets it right on the second try.

Partial streaming via Partial[Model] is another standout. You can stream partially-filled Pydantic objects as tokens arrive, which is essential for real-time UIs showing structured data. Multi-provider support through direct integrations or LiteLLM means you're never locked to a single vendor.

What's Not Great

It's a runtime approach. There's no compile-time type checking of your schema against what the LLM will actually return, you find errors at runtime. You're also tightly coupled to Pydantic, which is fine if you're already using it (most Python AI projects are) but adds a conceptual dependency if you aren't. The library also can't fix fundamentally broken LLM outputs, if the model returns markdown-wrapped JSON or chain-of-thought reasoning before the structured response, Instructor's strict JSON parser will choke. That's exactly the gap BAML fills.

Pricing

Completely free and open-source (MIT license). You pay only for your LLM API calls. No hosted tier, no premium features behind a paywall.

Who Should Use It

Any Python team that needs reliable structured output from LLMs. Solo developers, startups, enterprises, Instructor scales with you. If you're unsure which library to pick, this is the answer.

Verdict: no. 1 because it has the best ecosystem, the simplest API, and solves 90% of structured output needs. Start here unless you have a specific reason not to.

no. 2: Vercel AI SDK, The TypeScript Standard

The Vercel AI SDK is what Instructor is to Python, but for TypeScript. Its generateObject() and streamObject() functions take Zod schemas and return fully typed objects. If you're building anything in TypeScript or Next.js, this is the obvious pick.

What's Great

The integration with the TypeScript ecosystem is smooth. Zod plays the same role here that Pydantic plays in Python, it's the schema validation layer that generates JSON Schema from your TypeScript types. You get full type inference, so your IDE knows exactly what shape the returned object has. The SDK supports OpenAI, Anthropic, Google, and 20+ other providers out of the box, and the streaming story is excellent for building real-time UIs with React Server Components.

The broader ecosystem matters too. This isn't just a structured output tool, it's the dominant AI SDK for TypeScript with tight hooks into Next.js server actions, streaming responses, and tool calling. Your structured output code naturally integrates with the rest of your AI application.

What's Not Great

It's TypeScript-only. If your backend is Python (which is most ML/AI infrastructure), you'll need a separate solution there. The retry logic isn't as sophisticated as Instructor's, you don't get automatic re-prompting with validation errors out of the box. And while Zod schemas cover most use cases, very complex nested schemas with conditional logic can get verbose compared to Pydantic models.

Pricing

Free and open-source (Apache 2.0). No premium tier.

Who Should Use It

TypeScript and Next.js developers. If your stack is JavaScript/TypeScript end-to-end, there's genuinely no reason to look elsewhere for structured output.

Two alternatives worth knowing about: Instructor-TS ports the Instructor API pattern to TypeScript if you prefer that style. BAML-TS generates TypeScript clients from BAML schemas, the right call when your team uses both Python and TypeScript and wants a single schema definition.

FeatureVercel AI SDKInstructor-TSBAML-TS
StreamingstreamObject()Partial objectsNative streaming
Providers20+10+Any (via BAML config)
SchemaZodZodBAML DSL
EcosystemLargest TS AI ecosystemMirrors Python InstructorCross-language parity

Verdict: no. 2 because it's the undisputed TypeScript leader with excellent streaming, broad provider support, and tight Next.js integration.

no. 3: BAML, The Cross-Language Powerhouse

BAML from BoundaryML takes a fundamentally different approach than everything else on this list. You write .baml schema files in a purpose-built DSL, then generate typed clients for Python, TypeScript, Ruby, Java, Go, and Rust. Think Prisma for LLM structured output.

What's Great

The standout feature is Schema-Aligned Parsing (SAP). Where Instructor relies on strict JSON parsing, BAML handles the messy reality of LLM outputs, markdown embedded in JSON, chain-of-thought reasoning before the structured response, extra whitespace, trailing commas, and other quirks that break json.loads(). In our experience, this matters more than you'd expect. LLMs are sloppy, and BAML is built to handle that sloppiness gracefully.

Code generation means full IDE autocomplete and compile-time error catching across every supported language. If you have a Python backend and a TypeScript frontend, you define the schema once in BAML and get type-safe clients for both. That's genuinely hard to replicate with any other tool.

What's Not Great

You need a build step. Running baml-cli generate before your code can use the generated clients adds friction, especially in rapid prototyping. The DSL is another thing to learn, it's not complicated, but it's not Pydantic or Zod either. The community and ecosystem are smaller than Instructor's (5K+ stars vs 12K+), so you'll find fewer tutorials and Stack Overflow answers. And if you're a single-language Python shop, the cross-language benefit doesn't help you.

Pricing

Free and open-source (Apache 2.0). BoundaryML offers a hosted playground and testing tools, but the core library is free.

Who Should Use It

Teams working across multiple languages who want a single source of truth for their LLM schemas. Also a strong pick if your LLM outputs are messy and Instructor's strict JSON parsing isn't cutting it.

Verdict: no. 3 because the cross-language story and flexible parsing are genuinely unique. The build step friction keeps it from overtaking Instructor for single-language teams.

no. 4: Pydantic AI, Structured Output Meets Agents

Pydantic AI is the official agent framework from the Pydantic team, the same people behind the validation library that powers Instructor and most Python LLM tooling. Structured output isn't an add-on here; it's a core primitive baked into every agent.

What's Great

If you're building AI agents that need typed returns alongside tool calling, dependency injection, and complex workflows, everything lives under one roof. Agents return typed Pydantic models with automatic validation and re-prompting across 20+ providers. The framework includes streaming, graph-based workflows, and a testing story that most agent frameworks lack.

The Pydantic team's backing gives it credibility and staying power. These are the people who understand validation better than anyone in the Python ecosystem, and it shows in how the structured output layer integrates with everything else.

What's Not Great

Pydantic AI is broader than a structured output library, which is both its strength and weakness. If you just need to extract typed data from an LLM call, Instructor does it in fewer lines with less conceptual overhead. Pydantic AI's agent abstraction is extra machinery you don't need for simple extraction tasks. The library launched in late 2025, so the ecosystem is still maturing, fewer integrations, fewer examples, fewer battle-tested production deployments compared to Instructor.

Pricing

Free and open-source (MIT license). Logfire (Pydantic's observability platform) is a paid companion product but entirely optional.

Who Should Use It

Teams building AI agent systems in Python where structured output is one concern among many (tools, memory, workflows). If you're already planning to use an agent framework, Pydantic AI gives you structured output for free.

Verdict: no. 4 because it's the best option for agent-heavy architectures, but overkill if you just need structured extraction.

no. 5: XGrammar, The Invisible Engine

XGrammar operates at a completely different layer than everything above. While Instructor and BAML work after the LLM generates tokens (validate and retry), XGrammar works during token generation, masking invalid tokens so the model physically cannot produce malformed output. It's the default constrained decoding backend for vLLM, SGLang, and TensorRT-LLM.

What's Great

Zero-overhead structured output. Through vocabulary partitioning and adaptive token mask caching, XGrammar achieves up to 100x speedup over earlier constrained decoding approaches. The model outputs valid JSON on the first pass, every time, no retries, no wasted tokens. It supports JSON Schema, regex, and EBNF grammars, covering almost any output format you'd need.

If you're running self-hosted LLMs on vLLM or SGLang, you're already using XGrammar whether you know it or not. It's the built-in grammar engine.

What's Not Great

You can't use it with API providers like OpenAI or Anthropic, it's inference-server-level technology only. There's no direct Python API for casual use; it's designed to be embedded in serving frameworks, not called from application code. And constrained decoding can sometimes reduce output quality for complex schemas because the model can't "think" freely before structuring its output.

Pricing

Free and open-source (Apache 2.0).

Who Should Use It

Infrastructure engineers running self-hosted LLMs on vLLM, SGLang, or TensorRT-LLM who need guaranteed structured output with zero latency overhead.

Verdict: no. 5 because it's the fastest way to get structured output from self-hosted models, but irrelevant if you're using hosted API providers.

no. 6: Outlines, The Hackable Alternative

Outlines from dottxt is a Python-native constrained decoding library using FSM-based token masking. It compiles schemas into index structures for O(1) valid token lookup per generation step.

What's Great

It's far more accessible than XGrammar if you want a Python API you can actually call from application code. You can experiment with custom grammars, regex patterns, and JSON Schema constraints directly in a Python script. It works with transformers, vLLM, and llama.cpp, so you have flexibility across serving frameworks. The 10K+ GitHub stars and active community mean good documentation and support.

What's Not Great

Slower than XGrammar for production inference workloads (XGrammar's C++/Rust implementation and vocabulary partitioning give it a significant edge). If you're already using vLLM or SGLang, XGrammar is built-in, adding Outlines is an extra dependency that's slower. The library is best suited for experimentation and custom grammar use cases rather than high-throughput production serving.

FeatureXGrammarOutlines
LanguageC++/RustPython
IntegrationvLLM, SGLang, TensorRT-LLM (built-in)transformers, vLLM, llama.cpp
PerformanceUp to 100x faster (vocab partitioning)Fast (FSM indexing)
Ease of UseEngine-level (less direct API)Python-native, hackable
Best ForProduction inference serversStructured generation experiments

Pricing

Free and open-source (Apache 2.0). dottxt offers a hosted API, but the library itself is free.

Who Should Use It

Researchers and developers who want a Python-native constrained decoding library for experimentation, custom grammars, or self-hosted LLM prototyping.

Verdict: no. 6 because it's the most accessible constrained decoding library, but XGrammar beats it for production self-hosted deployments.

no. 7: LiteLLM, The Universal Adapter

LiteLLM isn't a structured output library per se, it's a unified proxy that gives you an OpenAI-compatible API across 100+ providers. But it earns a spot on this list because pairing LiteLLM with Instructor is one of the most powerful structured output setups available.

What's Great

One API for everything. OpenAI, Anthropic, Gemini, Mistral, Cohere, Azure, Bedrock, Ollama, and dozens more, all through the same completion() call. Since Instructor supports LiteLLM as a backend, you get automatic retries and Pydantic validation across every provider LiteLLM supports. It also includes cost tracking, load balancing, rate limiting, and a proxy server mode for team use.

What's Not Great

It adds a layer of abstraction that can make debugging harder. When something goes wrong, you're diagnosing through two libraries instead of one. LiteLLM also doesn't handle structured output itself, you still need Instructor (or manual JSON Schema handling) on top. And the provider compatibility matrix isn't always perfect; edge cases with newer providers or features can lag behind.

Pricing

Free and open-source core. LiteLLM offers a hosted proxy with team management features, but the library is free.

Who Should Use It

Teams that use multiple LLM providers and want to avoid vendor lock-in. Pair it with Instructor for the best multi-provider structured output experience. For broader stack decisions, see our AI stack guide for SaaS.

Verdict: no. 7 because it's the glue layer, not the structured output layer. Essential for multi-provider setups, but always used alongside Instructor.

no. 8: Marvin, The Quick Prototype Tool

Marvin offers the simplest structured output API in the Python ecosystem: cast(), extract(), and classify(). You pass in data and a type, and Marvin handles the rest.

What's Great

It's ridiculously fast to get started. Ten lines of code gets you working structured extraction. The API is so intuitive that you barely need documentation. For prototyping, demos, and quick scripts, nothing is faster.

What's Not Great

It's primarily OpenAI-only, which is a dealbreaker for production multi-provider setups. The simple API that makes prototyping fast becomes limiting when you need custom retry logic, partial streaming, or complex validation. The project has seen less active development compared to Instructor and BAML, and the ecosystem around it is small.

Pricing

Free and open-source (Apache 2.0).

Who Should Use It

Developers who need structured extraction working in five minutes for a prototype, demo, or internal tool where OpenAI is the only provider.

Verdict: no. 8 because it trades capability for simplicity. Perfect for prototyping, but you'll outgrow it fast.

Do You Even Need a Structured Output Library?

Honest answer: maybe not. The native provider SDKs have gotten surprisingly capable.

OpenAI's .parse() with Strict Mode guarantees 100% JSON Schema compliance. Anthropic's output_config supports JSON Schema directly. Google Gemini has response_schema. If you're locked to a single provider, working with simple flat schemas, and don't need retry logic or partial streaming, the native SDK is genuinely enough. Zero extra dependencies.

You need a library when things get real: multi-provider support (so you're not locked in), automatic retries with validation feedback (the LLM sees what it got wrong), partial streaming of nested objects, or complex schemas that need cross-language type safety. And if you're interested in how function calling relates to structured outputs, the approaches are complementary, structured output for data extraction, function calling for actions.

Verdict: If you're using one provider with simple schemas, start with the native SDK. Add Instructor or BAML when you hit its limits.

Why Techsy Picks Instructor as no. 1

We've shipped production structured output pipelines with Instructor, BAML, and the Vercel AI SDK across client projects. Here's why Instructor keeps winning for us:

  1. Fastest time-to-working-code. A new developer on the team can add a structured extraction endpoint in under an hour. With BAML, the DSL learning curve and build step add a day.
  2. The retry loop is magic. Instructor's automatic retry with validation feedback recovers from bad LLM outputs without any custom error handling code. In our experience, retry recovery rates sit above 95% for schemas under 15 fields.
  3. Provider flexibility matters in practice. We regularly switch between OpenAI (for speed), Anthropic (for complex reasoning), and local models (for cost) within the same project. Instructor + LiteLLM makes that trivial.
  4. The ecosystem answers your questions. When we hit edge cases, there's almost always an existing example, GitHub issue, or blog post covering it. BAML and Pydantic AI are catching up, but Instructor's head start is real.

That said, we switch to BAML for cross-language projects and Pydantic AI when the project is agent-heavy. There's no one-size-fits-all answer, just a solid default.

How Should You Choose? Decision Framework

Find your row and you're done.

If You Need...Use ThisWhy
Simple Python extraction, any providerInstructor (no. 1)Largest ecosystem, easiest setup, 15+ providers
TypeScript / Next.js projectVercel AI SDK (no. 2)Native TS, Zod schemas, streaming, 20+ providers
Cross-language teams (Python + TS + others)BAML (no. 3)Single schema, generated clients for 6 languages
AI agents with structured returnsPydantic AI (no. 4)Agent framework with typed output as core primitive
Self-hosted LLMs (vLLM, SGLang)XGrammar (no. 5)Default engine, 100x faster constrained decoding
Self-hosted with Python APIOutlines (no. 6)Python-native FSM-based structured generation
Multi-provider abstractionLiteLLM (no. 7) + Instructor (no. 1)Unified API across 100+ providers
Quick prototype, OpenAI onlyMarvin (no. 8)Simplest API: cast(), extract(), classify()
Single provider, simple schemasNative SDKNo dependency needed

Need Something Custom?

If you're building an AI product and aren't sure how structured output fits into your architecture, or you need help choosing between these tools for a specific use case, that's exactly the kind of problem we solve. We've built structured output pipelines for extraction, classification, and multi-step agent systems across different LLM providers. See our AI integration services. Reach out for a free technical consultation.

FAQ

What is the best library for LLM structured output?

For Python, Instructor is our no. 1 pick, it has the largest ecosystem, the most provider support, and the simplest API. For TypeScript, Vercel AI SDK with Zod schemas is the clear leader. The right choice depends on your language, provider needs, and whether you're building agents or doing extraction.

Should I use Instructor or BAML for structured output?

Instructor for quick setup and the largest ecosystem. BAML if you're working across multiple languages (Python + TypeScript + others) and want a single schema definition, or if your LLM outputs are messy and need BAML's flexible Schema-Aligned Parsing rather than strict JSON validation.

Is Instructor better than native OpenAI structured outputs?

Native OpenAI .parse() with Strict Mode works perfectly for single-provider setups with simple schemas. Instructor adds value through automatic retries with validation feedback, partial streaming, multi-provider support, and complex nested validation. If you only use OpenAI and your schemas are flat, the native SDK is genuinely enough.

What is Pydantic AI and how does it compare to Instructor?

Pydantic AI is an agent framework from the Pydantic team where structured output is a built-in primitive, not the sole focus. Instructor is laser-focused on extraction, define a model, get typed output. Choose Pydantic AI when you need agents with tools, dependency injection, and structured output working together. Choose Instructor when you just need reliable typed extraction.

How does Vercel AI SDK handle structured output?

Through generateObject() and streamObject() functions that accept Zod schemas. You define a Zod schema, pass it to the function along with a prompt, and get back a fully typed object. It supports 20+ providers including OpenAI, Anthropic, and Google, with built-in streaming of partial objects for real-time UIs.

What is XGrammar and when should I use it?

XGrammar is a constrained decoding engine, it operates at the inference server level to guarantee structured output by masking invalid tokens during generation. Use it if you run self-hosted LLMs on vLLM, SGLang, or TensorRT-LLM. It's already built into these servers as the default grammar backend. You don't use XGrammar with API-based providers like OpenAI.

How does Outlines compare to XGrammar?

Outlines is a Python library with a direct API; XGrammar is a C++/Rust engine embedded in inference servers. Outlines is more accessible for experimentation and custom grammars. XGrammar is faster (up to 100x through vocabulary partitioning) and already integrated into production inference stacks. For a production vLLM deployment, XGrammar is the default. For research and prototyping, Outlines gives you more control.

Can I use Instructor with Anthropic and Gemini?

Yes. Instructor supports 15+ providers directly, including Anthropic Claude, Google Gemini, Ollama, Mistral, and Cohere. For providers not directly supported, you can route through LiteLLM, which gives Instructor access to 100+ providers through a unified OpenAI-compatible API.

What is the best TypeScript library for structured LLM output?

Vercel AI SDK. It has the largest TypeScript AI ecosystem, native Zod schema support, streaming partial objects, and works with 20+ providers. Instructor-TS is a solid alternative if you prefer the Instructor API pattern. BAML-TS is the pick for teams that share schema definitions between Python and TypeScript services.

Do I need a structured output library or can I use the native API?

Native APIs (OpenAI Strict Mode, Anthropic output_config, Gemini response_schema) work well for single-provider setups with simple schemas. You should reach for a library when you need multi-provider support, automatic retries with validation feedback, streaming of partial objects, or cross-language type safety. The library adds a thin layer that pays for itself the first time an LLM returns malformed output and your app handles it gracefully instead of crashing.

Sources

Tags

structured output librariesinstructor libraryBAMLpydantic aivercel ai sdkxgrammaroutlinesllm tools

Share this article

Start Your Project

Ready to build something extraordinary?

Let's turn your vision into reality. Our team is ready to help you create software that makes a difference.