Structured Output

Get typed Python objects from LLMs using Pydantic, with automatic schema enforcement and retries across providers.

Core idea

Pass response_model=YourPydanticModel to generate() and receive a validated instance (or a validation error after retries).

Basic example

from pydantic import BaseModel
from abstractcore import create_llm

class Person(BaseModel):
    name: str
    age: int
    email: str

llm = create_llm("openai", model="gpt-5-mini")
person = llm.generate(
    "Extract: John Doe, 35 years old, john@example.com",
    response_model=Person,
)

assert isinstance(person, Person)
print(person.name, person.age, person.email)

Note: Structured output is designed for non-streaming calls (use stream=False, the default).

Automatic validation + retry

When a provider cannot enforce the schema natively, AbstractCore can retry with validation feedback.

from pydantic import BaseModel
from abstractcore import create_llm
from abstractcore.structured import FeedbackRetry

class Company(BaseModel):
    name: str
    website: str

llm = create_llm("ollama", model="qwen3:4b-instruct")
company = llm.generate(
    "Extract: ACME Corp, https://acme.example",
    response_model=Company,
    retry_strategy=FeedbackRetry(max_attempts=3),
)

Provider notes (native vs prompted)

AbstractCore selects a strategy automatically based on provider/model capabilities.

Provider Typical strategy How
OpenAI Native Schema passed to API (response_format)
Anthropic Native Schema enforced via tool-calling mechanism
Ollama Native Schema passed via format
LMStudio / OpenAI-compatible Native (when supported by backend) OpenAI-style schema payload
MLX / HuggingFace Prompted + retry Schema prompt + validation feedback loop (or native when available)

Tools + structured output (hybrid)

If you pass both tools=[...] and response_model=..., AbstractCore can use a hybrid flow (tool use, then structured follow-up).

For complex agent workflows, keep tool execution host-owned (execute_tools=false, default) and validate schemas at the boundary.

Related Documentation

API Reference

response_model and retry_strategy parameters

Tool Calling System

How tool calls are surfaced and executed

Examples

Copy/paste patterns (tools + structured output)