LLMBuilder provides a fluent API for constructing LLM clients that can return Pydantic-validated objects directly, without the overhead of the agent run lifecycle.
Use this pattern for classification, extraction, summarization, and any scenario where you want a single LLM call with a guaranteed output schema.
Example
The builder pattern
LLMBuilder uses a fluent (method-chaining) API to construct an LLM client with the exact configuration you need:
.build() call at the end constructs the final LLMClient with all specified settings.
Available builder methods:
Sampling controls are request fields, not builder methods. Set them on
LLMRequest, for example LLMRequest(..., temperature=0.0, max_tokens=1000).
Structured output with Pydantic
When you passresponse_model=YourModel to client.chat(), the client instructs the LLM to return output that conforms to the model’s JSON schema. The response is parsed and validated against the Pydantic model:
- If the LLM returns valid structured output,
resp.structured_responsecontains the parsed dictionary andresp.textcontains the raw response. - If the LLM returns output that does not match the schema, a
LLMInvalidResponseErroris raised.
response_format parameter) when available, with a fallback to prompt-based JSON extraction.
When to use LLMBuilder vs Runner
Use
LLMBuilder when you want precision and control over a single LLM interaction. Use Runner when you need the full agentic lifecycle.