Skip to main content
This guide covers two powerful features that enhance agent reliability and code organization: structured output for type-safe LLM responses, and tool context for dependency injection. 💻 Code Examples

Structured Output

Type-safe agent responses with Pydantic

Tool Context

Inject dependencies into tools

Structured Output

Structured output lets you define a Pydantic model as the expected response format from your agent. The LLM will return data that exactly matches your schema, giving you type-safe, validated data directly.

Basic Usage

You can set response_type on the agent (as a default for all runs) or pass it per-run:
Per-run response_type always overrides the agent’s default. This lets you have a sensible default while retaining flexibility.

How It Works

When you provide a response_type, Slide uses the output-tool pattern:
  1. Schema as Tool: Your Pydantic model is registered as a special “output tool”
  2. Forced Tool Call: The LLM is called with tool_choice="required", ensuring it must call a tool
  3. Validation: When the LLM calls the output tool, its arguments are validated against your schema
  4. Retry on Failure: If validation fails, an error message is added and the LLM retries
This approach has key advantages over simple JSON mode:
  • Tools + Structured Output: Regular tools work alongside structured output in the same conversation
  • Reliable Output: tool_choice="required" forces the model to respond with structured data
  • Cross-Provider Support: LiteLLM translates tool_choice for each provider (OpenAI, Anthropic, Gemini, Bedrock, etc.)
Azure OpenAI Limitation: Azure does not currently support tool_choice="required". If using Azure, set drop_params=True on your agent to gracefully fall back.

Complex Schemas

Structured output supports all Pydantic features:

Default Response Type

You can set a default response_type on the agent itself:

Automatic Retry on Validation Failure

Sometimes the LLM might return invalid JSON or data that doesn’t match your schema. Use RetryConfig to automatically retry with feedback:

How Retry Works

When validation fails and retry is enabled:
  1. Slide catches the ValidationError or JSONDecodeError from the output tool arguments
  2. A tool result message with the error details is added to the thread
  3. The LLM is called again with tool_choice="required" to try again
  4. This repeats until validation succeeds or max_retries is exhausted
  5. If all retries fail, StructuredOutputError is raised with the validation history

RetryConfig Options

The backoff is exponential: 1s → 2s → 4s. This prevents overwhelming the API during transient issues.

Tool Context (Dependency Injection)

Tool context lets you inject runtime dependencies (databases, API clients, user info) into your tools without hardcoding them.

Basic Usage

Context Parameter Convention

Tools receive context through a special first parameter. Use either:
  • ctx: ToolContext (recommended)
  • ctx: Dict[str, Any]
  • context: ToolContext
  • context: Dict[str, Any]
The context parameter MUST be the first parameter in your tool function signature. If it’s not first, the tool won’t receive the context.

Backward Compatibility

Tools without a context parameter continue to work normally:

Error Handling

If a tool expects context but none is provided:

Use Cases for Tool Context

Combining Features

Structured output and tool context work together seamlessly:

Streaming with Structured Output

Structured output is currently a non-streaming feature. When you use response_type, the agent will collect the full response before validating and returning.
If you need streaming with structured output, you can:
  1. Stream for real-time feedback, then parse the final response:
  1. Or use non-streaming mode for structured data:

Best Practices

Schema Design

1

Keep schemas focused

Define schemas for specific use cases rather than trying to capture everything:
2

Use Field constraints

Pydantic validators help the LLM produce correct output:
3

Provide descriptions

Field descriptions help the LLM understand what you want:

Tool Context

1

Use type hints

Even though context is a dict, document expected keys:
2

Validate early

Check for required keys at the start of your tool:
3

Keep context minimal

Only pass what’s needed:

Error Reference

Next Steps

Testing Agents

Test structured output and tool context

Agent API Reference

Complete Agent documentation

RetryConfig API

Retry configuration options

Advanced Patterns

More advanced usage patterns