# Pydantic AI > GenAI Agent Framework, the Pydantic way Pydantic AI is a Python agent framework designed to make it less painful to build production grade applications with Generative AI. --- # [Image, Audio, Video & Document Input](https://pydantic.dev/docs/ai/advanced-features/input/) # Image, Audio, Video & Document Input ## Image Input Note Some models do not support image input. Please check the model's documentation to confirm whether it supports image input. If you have a direct URL for the image, you can use [`ImageUrl`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.ImageUrl): image\_input.py ```python from pydantic_ai import Agent, ImageUrl agent = Agent(model='openai:gpt-5.2') result = agent.run_sync( [ 'What company is this logo from?', ImageUrl(url='https://iili.io/3Hs4FMg.png'), ] ) print(result.output) #> This is the logo for Pydantic, a data validation and settings management library in Python. ``` If you have the image locally, you can also use [`BinaryContent`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.BinaryContent): local\_image\_input.py ```python import httpx from pydantic_ai import Agent, BinaryContent image_response = httpx.get('https://iili.io/3Hs4FMg.png') # Pydantic logo agent = Agent(model='openai:gpt-5.2') result = agent.run_sync( [ 'What company is this logo from?', BinaryContent(data=image_response.content, media_type='image/png'), # (1) ] ) print(result.output) #> This is the logo for Pydantic, a data validation and settings management library in Python. ``` To ensure the example is runnable we download this image from the web, but you can also use `Path().read_bytes()` to read a local file's contents. ## Audio Input Note Some models do not support audio input. Please check the model's documentation to confirm whether it supports audio input. You can provide audio input using either [`AudioUrl`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.AudioUrl) or [`BinaryContent`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.BinaryContent). The process is analogous to the examples above. ## Video Input Note Some models do not support video input. Please check the model's documentation to confirm whether it supports video input. You can provide video input using either [`VideoUrl`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.VideoUrl) or [`BinaryContent`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.BinaryContent). The process is analogous to the examples above. ## Document Input Note Some models do not support document input. Please check the model's documentation to confirm whether it supports document input. You can provide document input using either [`DocumentUrl`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.DocumentUrl) or [`BinaryContent`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.BinaryContent). The process is similar to the examples above. If you have a direct URL for the document, you can use [`DocumentUrl`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.DocumentUrl): document\_input.py ```python from pydantic_ai import Agent, DocumentUrl agent = Agent(model='anthropic:claude-sonnet-4-6') result = agent.run_sync( [ 'What is the main content of this document?', DocumentUrl(url='https://storage.googleapis.com/cloud-samples-data/generative-ai/pdf/2403.05530.pdf'), ] ) print(result.output) #> This document is the technical report introducing Gemini 1.5, Google's latest large language model... ``` The supported document formats vary by model. You can also use [`BinaryContent`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.BinaryContent) to pass document data directly: binary\_content\_input.py ```python from pathlib import Path from pydantic_ai import Agent, BinaryContent pdf_path = Path('document.pdf') agent = Agent(model='anthropic:claude-sonnet-4-6') result = agent.run_sync( [ 'What is the main content of this document?', BinaryContent(data=pdf_path.read_bytes(), media_type='application/pdf'), ] ) print(result.output) #> The document discusses... ``` Tip If neither `DocumentUrl` nor `BinaryContent` is suitable for your use case (e.g., the model doesn't support `DocumentUrl`, or you want to provide a document in a non-binary format), you can still provide document content as text input by extracting the text yourself and passing it as a string or [`TextContent`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.TextContent). ## Text Input You can use [`TextContent`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.TextContent) to provide text input with additional metadata: text\_content\_input.py ```python from pydantic_ai import Agent, TextContent agent = Agent(model='openai:gpt-5.2') result = agent.run_sync([ 'Summarize the key points from this text.', TextContent( content=( 'Pydantic AI is a Python agent framework. ' 'It supports text, image, audio, video, and document input.' ), metadata={'source': 'pydantic_ai_inputs.txt'}, ), ]) ``` This is equivalent to passing the text as a `str`, but allows you to include additional `metadata` that can be accessed programmatically in your agent logic. Note The `content` field is treated as input to the model, but the `metadata` is **not sent to the model**. It is preserved in messages for programmatic access. ## User-side download vs. direct file URL When using one of `ImageUrl`, `AudioUrl`, `VideoUrl` or `DocumentUrl`, Pydantic AI will default to sending the URL to the model provider, so the file is downloaded on their side. Support for file URLs varies depending on type and provider: Model Send URL directly Download and send bytes Unsupported [`OpenAIChatModel`](/docs/ai/api/models/openai/#pydantic_ai.models.openai.OpenAIChatModel) `ImageUrl` `AudioUrl`, `DocumentUrl` `VideoUrl`. `DocumentUrl` [not supported with `AzureProvider`](/docs/ai/models/openai#using-azure-with-the-responses-api) [`OpenAIResponsesModel`](/docs/ai/api/models/openai/#pydantic_ai.models.openai.OpenAIResponsesModel) `ImageUrl`, `AudioUrl`, `DocumentUrl` -- `VideoUrl` [`AnthropicModel`](/docs/ai/api/models/anthropic/#pydantic_ai.models.anthropic.AnthropicModel) `ImageUrl`, `DocumentUrl` (PDF) `DocumentUrl` (`text/plain`) `AudioUrl`, `VideoUrl` [`GoogleModel`](/docs/ai/api/models/google/#pydantic_ai.models.google.GoogleModel) (Google Cloud) All URL types -- -- [`GoogleModel`](/docs/ai/api/models/google/#pydantic_ai.models.google.GoogleModel) (Gemini API) [YouTube](/docs/ai/models/google#document-image-audio-and-video-input), [Files API](/docs/ai/models/google#document-image-audio-and-video-input) All other URLs -- [`XaiModel`](/docs/ai/api/models/xai/#pydantic_ai.models.xai.XaiModel) `ImageUrl` `DocumentUrl` `AudioUrl`, `VideoUrl` [`MistralModel`](/docs/ai/api/models/mistral/#pydantic_ai.models.mistral.MistralModel) `ImageUrl`, `DocumentUrl` (PDF) -- `AudioUrl`, `VideoUrl`, `DocumentUrl` (non-PDF) [`BedrockConverseModel`](/docs/ai/api/models/bedrock/#pydantic_ai.models.bedrock.BedrockConverseModel) S3 URLs (`s3://`) `ImageUrl`, `DocumentUrl`, `VideoUrl` `AudioUrl` [`OpenRouterModel`](/docs/ai/api/models/openrouter/#pydantic_ai.models.openrouter.OpenRouterModel) `ImageUrl`, `DocumentUrl`, `VideoUrl` `AudioUrl` -- A model API may be unable to download a file (e.g., because of crawling or access restrictions) even if it supports file URLs. For example, [`GoogleModel`](/docs/ai/api/models/google/#pydantic_ai.models.google.GoogleModel) on Google Cloud limits YouTube video URLs to one URL per request. In such cases, you can instruct Pydantic AI to download the file content locally and send that instead of the URL by setting `force_download` on the URL object: force\_download.py ```py from pydantic_ai import ImageUrl, AudioUrl, VideoUrl, DocumentUrl ImageUrl(url='https://example.com/image.png', force_download=True) AudioUrl(url='https://example.com/audio.mp3', force_download=True) VideoUrl(url='https://example.com/video.mp4', force_download=True) DocumentUrl(url='https://example.com/doc.pdf', force_download=True) ``` Trust model for file URLs When URLs are forwarded to the provider, the provider fetches them under its own credentials. For cloud-storage schemes like `s3://` (Bedrock) and `gs://` (Google Cloud), those credentials are your server's IAM role or service account, so whoever controls the URL effectively controls what the provider can read on your behalf. Don't construct [`ImageUrl`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.ImageUrl), [`AudioUrl`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.AudioUrl), [`VideoUrl`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.VideoUrl), or [`DocumentUrl`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.DocumentUrl) from untrusted user input without validating the scheme and scope. For frontend-initiated uploads to cloud storage, convert references like `s3://bucket/key` into pre-signed `https://` URLs server-side before constructing the file URL part. `force_download=True` only works for `http(s)://` URLs (it routes through the library's HTTP client and applies SSRF protection); cloud-storage schemes like `s3://` and `gs://` aren't supported by the local download path and are forwarded to the provider as-is. The [UI adapters](/docs/ai/integrations/ui/overview) apply this sanitization automatically to client-submitted messages via [`UIAdapter.allowed_file_url_schemes`](/docs/ai/api/ui/base/#pydantic_ai.ui.UIAdapter.allowed_file_url_schemes). ## Uploaded Files Some model providers have their own file storage APIs where you can upload files and reference them by ID or URL. Use [`UploadedFile`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.UploadedFile) to reference files that have been uploaded to a provider's file storage API. Tip For providers that return a file URL (like Google Files API or S3 URLs for Bedrock), you can also use [`DocumentUrl`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.DocumentUrl), [`ImageUrl`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.ImageUrl), or [`VideoUrl`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.VideoUrl) directly. However, we recommend using `UploadedFile` for a unified API across providers and consistent provider name validation. ### Supported Models Model Support [`AnthropicModel`](/docs/ai/api/models/anthropic/#pydantic_ai.models.anthropic.AnthropicModel) ✅ via [Anthropic Files API](https://docs.anthropic.com/en/docs/build-with-claude/files) [`OpenAIChatModel`](/docs/ai/api/models/openai/#pydantic_ai.models.openai.OpenAIChatModel) ✅ via [OpenAI Files API](https://platform.openai.com/docs/api-reference/files) [`OpenAIResponsesModel`](/docs/ai/api/models/openai/#pydantic_ai.models.openai.OpenAIResponsesModel) ✅ via [OpenAI Files API](https://platform.openai.com/docs/api-reference/files) [`GoogleModel`](/docs/ai/api/models/google/#pydantic_ai.models.google.GoogleModel) ✅ via [Google Files API](https://ai.google.dev/gemini-api/docs/files) [`BedrockConverseModel`](/docs/ai/api/models/bedrock/#pydantic_ai.models.bedrock.BedrockConverseModel) ✅ via S3 URLs (`s3://bucket/key`) [`XaiModel`](/docs/ai/api/models/xai/#pydantic_ai.models.xai.XaiModel) ✅ via [xAI Files API](https://docs.x.ai/docs/guides/files) Other models ❌ Not supported ### Provider Name Requirement When using [`UploadedFile`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.UploadedFile) you must set the `provider_name`. Uploaded files are specific to the system they are uploaded to and are not transferable across providers. Trying to use a message that contains an `UploadedFile` with a different provider will result in an error. Getting the provider name Use [`model.system`](/docs/ai/api/models/base/#pydantic_ai.models.Model.system) to get the correct provider name dynamically. This ensures your code works correctly even if the provider name changes. All examples below demonstrate this pattern. If you want to introduce portability into your agent logic to allow the same prompt history to work with different provider backends, you can use a [history processor](/docs/ai/core-concepts/message-history#processing-message-history) to remove or rewrite `UploadedFile` parts from messages before sending them to a provider that does not support them. Be aware that stripping out `UploadedFile` instances might confuse the model, especially if references to those files remain in the text. ### Media Type Inference The `media_type` parameter is optional for [`UploadedFile`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.UploadedFile). If not specified, Pydantic AI will attempt to infer it from the `file_id`: 1. If `file_id` is a URL or path with a recognizable file extension (e.g., `.pdf`, `.png`), the media type is inferred automatically 2. For opaque file IDs (e.g., `'file-abc123'`), the media type defaults to `'application/octet-stream'` Tip While `media_type` is optional, we recommend explicitly setting it when known to ensure correct handling by the model provider. ### Anthropic Follow the [Anthropic Files API docs](https://docs.anthropic.com/en/docs/build-with-claude/files) to upload files. You can access the underlying Anthropic client via `provider.client`. Beta Feature The Anthropic Files API is currently in beta. You need to include the beta header `anthropic-beta: files-api-2025-04-14` when making requests. uploaded\_file\_anthropic.py ```py import asyncio from pydantic_ai import Agent, ModelSettings, UploadedFile from pydantic_ai.models.anthropic import AnthropicModel from pydantic_ai.providers.anthropic import AnthropicProvider async def main(): provider = AnthropicProvider() model = AnthropicModel('claude-sonnet-4-5', provider=provider) # Upload a file using the provider's client (Anthropic client) with open('document.pdf', 'rb') as f: uploaded_file = await provider.client.beta.files.upload(file=f) # Reference the uploaded file, including the required beta header agent = Agent(model) result = await agent.run( [ 'Summarize this document', UploadedFile(file_id=uploaded_file.id, provider_name=model.system), ], model_settings=ModelSettings(extra_headers={'anthropic-beta': 'files-api-2025-04-14'}), ) print(result.output) #> The document discusses the main topics and key findings... asyncio.run(main()) ``` ### OpenAI Follow the [OpenAI Files API docs](https://platform.openai.com/docs/api-reference/files/create) to upload files. You can access the underlying OpenAI client via `provider.client`. uploaded\_file\_openai.py ```py import asyncio from pydantic_ai import Agent, UploadedFile from pydantic_ai.models.openai import OpenAIChatModel from pydantic_ai.providers.openai import OpenAIProvider async def main(): provider = OpenAIProvider() model = OpenAIChatModel('gpt-5', provider=provider) # Upload a file using the provider's client (OpenAI client) with open('document.pdf', 'rb') as f: uploaded_file = await provider.client.files.create(file=f, purpose='user_data') # Reference the uploaded file agent = Agent(model) result = await agent.run( [ 'Summarize this document', UploadedFile(file_id=uploaded_file.id, provider_name=model.system), ] ) print(result.output) #> The document discusses the main topics and key findings... asyncio.run(main()) ``` ### Google Follow the [Google Files API docs](https://ai.google.dev/gemini-api/docs/files) to upload files. You can access the underlying Google GenAI client via `provider.client`. uploaded\_file\_google.py ```py import asyncio from pydantic_ai import Agent, UploadedFile from pydantic_ai.models.google import GoogleModel from pydantic_ai.providers.google import GoogleProvider async def main(): provider = GoogleProvider() model = GoogleModel('gemini-2.5-flash', provider=provider) # Upload a file using the provider's client (Google GenAI client) with open('document.pdf', 'rb') as f: file = await provider.client.aio.files.upload(file=f) assert file.uri is not None # Reference the uploaded file by URI (media_type is optional for Google) agent = Agent(model) result = await agent.run( [ 'Summarize this document', UploadedFile(file_id=file.uri, media_type=file.mime_type, provider_name=model.system), ] ) print(result.output) #> The document discusses the main topics and key findings... asyncio.run(main()) ``` ### Bedrock (S3) For Bedrock, files must be uploaded to S3 separately (e.g., using [boto3](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3/client/put_object.html)). The assumed role must have `s3:GetObject` permission on the bucket. `media_type` may be required Bedrock requires `media_type` when the file extension is ambiguous or missing. For S3 URLs with clear extensions like `.pdf`, `.png`, etc., it can be inferred automatically. uploaded\_file\_bedrock.py ```py import asyncio from pydantic_ai import Agent, UploadedFile from pydantic_ai.models.bedrock import BedrockConverseModel async def main(): model = BedrockConverseModel('us.anthropic.claude-sonnet-4-20250514-v1:0') agent = Agent(model) result = await agent.run([ 'Summarize this document', UploadedFile( file_id='s3://my-bucket/document.pdf', provider_name=model.system, # 'bedrock' media_type='application/pdf', # Optional for .pdf, but recommended ), ]) print(result.output) #> The document discusses the main topics and key findings... asyncio.run(main()) ``` Note You can optionally specify a `bucketOwner` query parameter if the bucket is not owned by the account making the request: `s3://my-bucket/document.pdf?bucketOwner=123456789012` ### xAI Follow the [xAI Files API docs](https://docs.x.ai/docs/guides/files) to upload files. You can access the underlying xAI client via `provider.client`. uploaded\_file\_xai.py ```py import asyncio from pydantic_ai import Agent, UploadedFile from pydantic_ai.models.xai import XaiModel from pydantic_ai.providers.xai import XaiProvider async def main(): provider = XaiProvider() model = XaiModel('grok-4-fast', provider=provider) # Upload a file using the provider's client (xAI client) with open('document.pdf', 'rb') as f: uploaded_file = await provider.client.files.upload(f, filename='document.pdf') # Reference the uploaded file agent = Agent(model) result = await agent.run( [ 'Summarize this document', UploadedFile(file_id=uploaded_file.id, provider_name=model.system), ] ) print(result.output) #> The document discusses the main topics and key findings... asyncio.run(main()) ``` --- # [HTTP Request Retries](https://pydantic.dev/docs/ai/advanced-features/retries/) # HTTP Request Retries Pydantic AI provides retry functionality for HTTP requests made by model providers through custom HTTP transports. This is particularly useful for handling transient failures like rate limits, network timeouts, or temporary server errors. The retry functionality is built on top of the [tenacity](https://github.com/jd/tenacity) library and integrates seamlessly with httpx clients. You can configure retry behavior for any provider that accepts a custom HTTP client. ## Installation To use the retry transports, you need to install `tenacity`, which you can do via the `retries` dependency group: - [pip](#tab-panel-4) - [uv](#tab-panel-5) Terminal ```bash pip install 'pydantic-ai-slim[retries]' ``` Terminal ```bash uv add 'pydantic-ai-slim[retries]' ``` ## Usage Example Here's an example of adding retry functionality with smart retry handling: smart\_retry\_example.py ```python from httpx import AsyncClient, HTTPStatusError from tenacity import retry_if_exception_type, stop_after_attempt, wait_exponential from pydantic_ai import Agent from pydantic_ai.models.openai import OpenAIChatModel from pydantic_ai.providers.openai import OpenAIProvider from pydantic_ai.retries import AsyncTenacityTransport, RetryConfig, wait_retry_after def create_retrying_client(): """Create a client with smart retry handling for multiple error types.""" def should_retry_status(response): """Raise exceptions for retryable HTTP status codes.""" if response.status_code in (429, 502, 503, 504): response.raise_for_status() # This will raise HTTPStatusError transport = AsyncTenacityTransport( config=RetryConfig( # Retry on HTTP errors and connection issues retry=retry_if_exception_type((HTTPStatusError, ConnectionError)), # Smart waiting: respects Retry-After headers, falls back to exponential backoff wait=wait_retry_after( fallback_strategy=wait_exponential(multiplier=1, max=60), max_wait=300 ), # Stop after 5 attempts stop=stop_after_attempt(5), # Re-raise the last exception if all retries fail reraise=True ), validate_response=should_retry_status ) return AsyncClient(transport=transport) # Use the retrying client with a model client = create_retrying_client() model = OpenAIChatModel('gpt-5.2', provider=OpenAIProvider(http_client=client)) agent = Agent(model) ``` ## Wait Strategies ### wait\_retry\_after The `wait_retry_after` function is a smart wait strategy that automatically respects HTTP `Retry-After` headers: wait\_strategy\_example.py ```python from tenacity import wait_exponential from pydantic_ai.retries import wait_retry_after # Basic usage - respects Retry-After headers, falls back to exponential backoff wait_strategy_1 = wait_retry_after() # Custom configuration wait_strategy_2 = wait_retry_after( fallback_strategy=wait_exponential(multiplier=2, max=120), max_wait=600 # Never wait more than 10 minutes ) ``` This wait strategy: - Automatically parses `Retry-After` headers from HTTP 429 responses - Supports both seconds format (`"30"`) and HTTP date format (`"Wed, 21 Oct 2015 07:28:00 GMT"`) - Falls back to your chosen strategy when no header is present - Respects the `max_wait` limit to prevent excessive delays ## Transport Classes ### AsyncTenacityTransport For asynchronous HTTP clients (recommended for most use cases): async\_transport\_example.py ```python from httpx import AsyncClient from tenacity import stop_after_attempt from pydantic_ai.retries import AsyncTenacityTransport, RetryConfig def validator(response): """Treat responses with HTTP status 4xx/5xx as failures that need to be retried. Without a response validator, only network errors and timeouts will result in a retry. """ response.raise_for_status() # Create the transport transport = AsyncTenacityTransport( config=RetryConfig(stop=stop_after_attempt(3), reraise=True), validate_response=validator ) # Create a client using the transport: client = AsyncClient(transport=transport) ``` ### TenacityTransport For synchronous HTTP clients: sync\_transport\_example.py ```python from httpx import Client from tenacity import stop_after_attempt from pydantic_ai.retries import RetryConfig, TenacityTransport def validator(response): """Treat responses with HTTP status 4xx/5xx as failures that need to be retried. Without a response validator, only network errors and timeouts will result in a retry. """ response.raise_for_status() # Create the transport transport = TenacityTransport( config=RetryConfig(stop=stop_after_attempt(3), reraise=True), validate_response=validator ) # Create a client using the transport client = Client(transport=transport) ``` ## Common Retry Patterns ### Rate Limit Handling with Retry-After Support rate\_limit\_handling.py ```python from httpx import AsyncClient, HTTPStatusError from tenacity import retry_if_exception_type, stop_after_attempt, wait_exponential from pydantic_ai.retries import AsyncTenacityTransport, RetryConfig, wait_retry_after def create_rate_limit_client(): """Create a client that respects Retry-After headers from rate limiting responses.""" transport = AsyncTenacityTransport( config=RetryConfig( retry=retry_if_exception_type(HTTPStatusError), wait=wait_retry_after( fallback_strategy=wait_exponential(multiplier=1, max=60), max_wait=300 # Don't wait more than 5 minutes ), stop=stop_after_attempt(10), reraise=True ), validate_response=lambda r: r.raise_for_status() # Raises HTTPStatusError for 4xx/5xx ) return AsyncClient(transport=transport) # Example usage client = create_rate_limit_client() # Client is now ready to use with any HTTP requests and will respect Retry-After headers ``` The `wait_retry_after` function automatically detects `Retry-After` headers in 429 (rate limit) responses and waits for the specified time. If no header is present, it falls back to exponential backoff. ### Network Error Handling network\_error\_handling.py ```python import httpx from tenacity import retry_if_exception_type, stop_after_attempt, wait_exponential from pydantic_ai.retries import AsyncTenacityTransport, RetryConfig def create_network_resilient_client(): """Create a client that handles network errors with retries.""" transport = AsyncTenacityTransport( config=RetryConfig( retry=retry_if_exception_type(( httpx.TimeoutException, httpx.ConnectError, httpx.ReadError )), wait=wait_exponential(multiplier=1, max=10), stop=stop_after_attempt(3), reraise=True ) ) return httpx.AsyncClient(transport=transport) # Example usage client = create_network_resilient_client() # Client will now retry on timeout, connection, and read errors ``` ### Custom Retry Logic custom\_retry\_logic.py ```python import httpx from tenacity import retry_if_exception, stop_after_attempt, wait_exponential from pydantic_ai.retries import AsyncTenacityTransport, RetryConfig, wait_retry_after def create_custom_retry_client(): """Create a client with custom retry logic.""" def custom_retry_condition(exception): """Custom logic to determine if we should retry.""" if isinstance(exception, httpx.HTTPStatusError): # Retry on server errors but not client errors return 500 <= exception.response.status_code < 600 return isinstance(exception, httpx.TimeoutException | httpx.ConnectError) transport = AsyncTenacityTransport( config=RetryConfig( retry=retry_if_exception(custom_retry_condition), # Use wait_retry_after for smart waiting on rate limits, # with custom exponential backoff as fallback wait=wait_retry_after( fallback_strategy=wait_exponential(multiplier=2, max=30), max_wait=120 ), stop=stop_after_attempt(5), reraise=True ), validate_response=lambda r: r.raise_for_status() ) return httpx.AsyncClient(transport=transport) client = create_custom_retry_client() # Client will retry server errors (5xx) and network errors, but not client errors (4xx) ``` ## Using with Different Providers The retry transports work with any provider that accepts a custom HTTP client: ### OpenAI openai\_with\_retries.py ```python from pydantic_ai import Agent from pydantic_ai.models.openai import OpenAIChatModel from pydantic_ai.providers.openai import OpenAIProvider from smart_retry_example import create_retrying_client client = create_retrying_client() model = OpenAIChatModel('gpt-5.2', provider=OpenAIProvider(http_client=client)) agent = Agent(model) ``` ### Anthropic anthropic\_with\_retries.py ```python from pydantic_ai import Agent from pydantic_ai.models.anthropic import AnthropicModel from pydantic_ai.providers.anthropic import AnthropicProvider from smart_retry_example import create_retrying_client client = create_retrying_client() model = AnthropicModel('claude-sonnet-4-5-20250929', provider=AnthropicProvider(http_client=client)) agent = Agent(model) ``` ### Any OpenAI-Compatible Provider openai\_compatible\_with\_retries.py ```python from pydantic_ai import Agent from pydantic_ai.models.openai import OpenAIChatModel from pydantic_ai.providers.openai import OpenAIProvider from smart_retry_example import create_retrying_client client = create_retrying_client() model = OpenAIChatModel( 'your-model-name', # Replace with actual model name provider=OpenAIProvider( base_url='https://api.example.com/v1', # Replace with actual API URL api_key='your-api-key', # Replace with actual API key http_client=client ) ) agent = Agent(model) ``` ## Best Practices 1. **Start Conservative**: Begin with a small number of retries (3-5) and reasonable wait times. 2. **Use Exponential Backoff**: This helps avoid overwhelming servers during outages. 3. **Set Maximum Wait Times**: Prevent indefinite delays with reasonable maximum wait times. 4. **Handle Rate Limits Properly**: Respect `Retry-After` headers when possible. 5. **Log Retry Attempts**: Add logging to monitor retry behavior in production. (This will be picked up by Logfire automatically if you instrument httpx.) 6. **Consider Circuit Breakers**: For high-traffic applications, consider implementing circuit breaker patterns. Monitoring Retries in Production Excessive retries can indicate underlying issues and increase costs. [Logfire](/docs/ai/integrations/logfire) helps you track retry patterns: - See which requests triggered retries - Understand retry causes (rate limits, server errors, timeouts) - Monitor retry frequency over time - Identify opportunities to reduce retries With [HTTPX instrumentation](/docs/ai/integrations/logfire#monitoring-http-requests) enabled, retry attempts are automatically captured in your traces. ## Error Handling The retry transports will re-raise the last exception if all retry attempts fail. Make sure to handle these appropriately in your application: error\_handling\_example.py ```python from pydantic_ai import Agent from pydantic_ai.models.openai import OpenAIChatModel from pydantic_ai.providers.openai import OpenAIProvider from smart_retry_example import create_retrying_client client = create_retrying_client() model = OpenAIChatModel('gpt-5.2', provider=OpenAIProvider(http_client=client)) agent = Agent(model) ``` ## Performance Considerations - Retries add latency to requests, especially with exponential backoff - Consider the total timeout for your application when configuring retry behavior - Monitor retry rates to detect systemic issues - Use async transports for better concurrency when handling multiple requests For more advanced retry configurations, refer to the [tenacity documentation](https://tenacity.readthedocs.io/). ## Provider-Specific Retry Behavior ### AWS Bedrock The AWS Bedrock provider uses boto3's built-in retry mechanisms instead of httpx. To configure retries for Bedrock, use boto3's `Config`: ```python from botocore.config import Config config = Config(retries={'max_attempts': 5, 'mode': 'adaptive'}) ``` See [Bedrock: Configuring Retries](/docs/ai/models/bedrock#configuring-retries) for complete examples. --- # [Thinking](https://pydantic.dev/docs/ai/advanced-features/thinking/) # Thinking Thinking (or reasoning) is the process by which a model works through a problem step-by-step before providing its final answer. This capability is typically disabled by default and depends on the specific model being used. See the sections below for how to enable thinking for each provider. ## Unified thinking settings The simplest way to enable thinking across any supported provider is the `thinking` field in [`ModelSettings`](/docs/ai/api/pydantic-ai/settings/#pydantic_ai.settings.ModelSettings): unified\_thinking.py ```python from pydantic_ai import Agent agent = Agent('anthropic:claude-opus-4-7', model_settings={'thinking': 'high'}) ``` Or using the [`Thinking`](/docs/ai/api/pydantic-ai/capabilities/#pydantic_ai.capabilities.Thinking) capability: thinking\_capability.py ```python from pydantic_ai import Agent from pydantic_ai.capabilities import Thinking agent = Agent('anthropic:claude-opus-4-7', capabilities=[Thinking(effort='high')]) ``` The `thinking` setting accepts: - `True` -- enable thinking with the provider's default effort level - `False` -- disable thinking (silently ignored on always-on models) - `'minimal'` / `'low'` / `'medium'` / `'high'` / `'xhigh'` -- enable thinking at a specific effort level (unsupported levels map to the closest available value) When omitted, the model uses its default behavior. Provider-specific settings (documented in the sections below) take precedence when both are set. ### Provider translation The unified `thinking` setting maps to each provider's native format: Provider `thinking=True` `thinking='high'` Notes Anthropic (Opus 4.6+) `anthropic_thinking={'type': 'adaptive'}` `{type: 'adaptive'}` + `effort='high'` Claude Opus 4.7 also supports `effort='xhigh'` Anthropic (older) `anthropic_thinking={'type': 'enabled', 'budget_tokens': 10000}` `budget_tokens=16384` Budget-based; `'low'` → 2048 tokens OpenAI `reasoning_effort='medium'` `reasoning_effort='high'` Google (Gemini 3+) `include_thoughts=True` `thinking_level='HIGH'` Google (Gemini 2.5) `include_thoughts=True` `thinking_budget=24576` Groq `reasoning_format='parsed'` `reasoning_format='parsed'` `thinking=False` → `'hidden'` (no true disable) OpenRouter `reasoning.effort='medium'` `reasoning.effort='high'` Via `extra_body` Cerebras `disable_reasoning=False` `disable_reasoning=False` `thinking=False` → `disable_reasoning=True` xAI `reasoning_effort='high'` `reasoning_effort='high'` Only `'low'` and `'high'` Bedrock (Claude) `thinking.type='enabled'` `budget_tokens=16384` No adaptive support Bedrock (OpenAI) `reasoning_effort='medium'` `reasoning_effort='high'` ## OpenAI When using the [`OpenAIChatModel`](/docs/ai/api/models/openai/#pydantic_ai.models.openai.OpenAIChatModel), text output inside `` tags are converted to [`ThinkingPart`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.ThinkingPart) objects. You can customize the tags using the [`thinking_tags`](/docs/ai/api/pydantic-ai/profiles/#pydantic_ai.profiles.ModelProfile.thinking_tags) field on the [model profile](/docs/ai/models/openai#model-profile). Some [OpenAI-compatible model providers](/docs/ai/models/openai#openai-compatible-models) might also support native thinking parts that are not delimited by tags. Instead, they are sent and received as separate, custom fields in the API. Typically, if you are calling the model via the `:` shorthand, Pydantic AI handles it for you. Nonetheless, you can still configure the fields with [`openai_chat_thinking_field`](/docs/ai/api/pydantic-ai/profiles/#pydantic_ai.profiles.openai.OpenAIModelProfile.openai_chat_thinking_field). If your provider recommends to send back these custom fields not changed, for caching or interleaved thinking benefits, you can also achieve this with [`openai_chat_send_back_thinking_parts`](/docs/ai/api/pydantic-ai/profiles/#pydantic_ai.profiles.openai.OpenAIModelProfile.openai_chat_send_back_thinking_parts). ### OpenAI Responses The [`OpenAIResponsesModel`](/docs/ai/api/models/openai/#pydantic_ai.models.openai.OpenAIResponsesModel) can generate native thinking parts. To enable this functionality, you need to set the `OpenAIResponsesModelSettings.openai_reasoning_effort` and [`OpenAIResponsesModelSettings.openai_reasoning_summary`](/docs/ai/api/models/openai/#pydantic_ai.models.openai.OpenAIResponsesModelSettings.openai_reasoning_summary) [model settings](/docs/ai/core-concepts/agent#model-run-settings). By default, the unique IDs of reasoning, text, and function call parts from the message history are sent to the model, which can result in errors like `"Item 'rs_123' of type 'reasoning' was provided without its required following item."` if the message history you're sending does not match exactly what was received from the Responses API in a previous response, for example if you're using a [history processor](/docs/ai/core-concepts/message-history#processing-message-history). To disable this, you can disable the [`OpenAIResponsesModelSettings.openai_send_reasoning_ids`](/docs/ai/api/models/openai/#pydantic_ai.models.openai.OpenAIResponsesModelSettings.openai_send_reasoning_ids) [model setting](/docs/ai/core-concepts/agent#model-run-settings). openai\_thinking\_part.py ```python from pydantic_ai import Agent from pydantic_ai.models.openai import OpenAIResponsesModel, OpenAIResponsesModelSettings model = OpenAIResponsesModel('gpt-5.2') settings = OpenAIResponsesModelSettings( openai_reasoning_effort='low', openai_reasoning_summary='detailed', ) agent = Agent(model, model_settings=settings) ... ``` Raw reasoning without summaries Some OpenAI-compatible APIs (such as LM Studio, vLLM, or OpenRouter with gpt-oss models) may return raw reasoning content without reasoning summaries. In this case, [`ThinkingPart.content`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.ThinkingPart.content) will be empty, but the raw reasoning is available in `provider_details['raw_content']`. Following [OpenAI's guidance](https://cookbook.openai.com/examples/responses_api/reasoning_items) that raw reasoning should not be shown directly to users, we store it in `provider_details` rather than in the main `content` field. ## Anthropic To enable thinking, use the [`AnthropicModelSettings.anthropic_thinking`](/docs/ai/api/models/anthropic/#pydantic_ai.models.anthropic.AnthropicModelSettings.anthropic_thinking) [model setting](/docs/ai/core-concepts/agent#model-run-settings). Note Extended thinking (`type: 'enabled'` with `budget_tokens`) is deprecated on `claude-opus-4-6` and removed on `claude-opus-4-7`+. For those models, use [adaptive thinking](#adaptive-thinking--effort) instead. anthropic\_thinking\_part.py ```python from pydantic_ai import Agent from pydantic_ai.models.anthropic import AnthropicModel, AnthropicModelSettings model = AnthropicModel('claude-sonnet-4-5') settings = AnthropicModelSettings( anthropic_thinking={'type': 'enabled', 'budget_tokens': 1024}, ) agent = Agent(model, model_settings=settings) ... ``` ### Interleaved Thinking To enable [interleaved thinking](https://docs.anthropic.com/en/docs/build-with-claude/extended-thinking#interleaved-thinking), you need to include the beta header in your model settings: anthropic\_interleaved\_thinking.py ```python from pydantic_ai import Agent from pydantic_ai.models.anthropic import AnthropicModel, AnthropicModelSettings model = AnthropicModel('claude-sonnet-4-5') settings = AnthropicModelSettings( anthropic_thinking={'type': 'enabled', 'budget_tokens': 10000}, extra_headers={'anthropic-beta': 'interleaved-thinking-2025-05-14'}, ) agent = Agent(model, model_settings=settings) ... ``` ### Adaptive Thinking & Effort Starting with `claude-opus-4-6`, Anthropic supports [adaptive thinking](https://docs.anthropic.com/en/docs/build-with-claude/adaptive-thinking), where the model dynamically decides when and how much to think based on the complexity of each request. This replaces extended thinking (`type: 'enabled'` with `budget_tokens`) which is deprecated on Opus 4.6 and removed on Opus 4.7. Claude Opus 4.7 also adds the `xhigh` effort level. Adaptive thinking also automatically enables interleaved thinking. anthropic\_adaptive\_thinking.py ```python from pydantic_ai import Agent from pydantic_ai.models.anthropic import AnthropicModel, AnthropicModelSettings model = AnthropicModel('claude-opus-4-7') settings = AnthropicModelSettings( anthropic_thinking={'type': 'adaptive'}, anthropic_effort='high', ) agent = Agent(model, model_settings=settings) ... ``` The [`anthropic_effort`](/docs/ai/api/models/anthropic/#pydantic_ai.models.anthropic.AnthropicModelSettings.anthropic_effort) setting controls how much effort the model puts into its response (independent of thinking). See the [Anthropic effort docs](https://docs.anthropic.com/en/docs/build-with-claude/effort) for details. Note Older models (`claude-sonnet-4-5`, `claude-opus-4-5`, etc.) do not support adaptive thinking and require `{'type': 'enabled', 'budget_tokens': N}` as shown [above](#anthropic). Thinking tokens count against Anthropic's loop-wide [task budgets](/docs/ai/models/anthropic#task-budgets-beta), so adaptive thinking naturally scales down as the budget depletes. ## Google To enable thinking, use the [`GoogleModelSettings.google_thinking_config`](/docs/ai/api/models/google/#pydantic_ai.models.google.GoogleModelSettings.google_thinking_config) [model setting](/docs/ai/core-concepts/agent#model-run-settings). google\_thinking\_part.py ```python from pydantic_ai import Agent from pydantic_ai.models.google import GoogleModel, GoogleModelSettings model = GoogleModel('gemini-3-pro-preview') settings = GoogleModelSettings(google_thinking_config={'include_thoughts': True}) agent = Agent(model, model_settings=settings) ... ``` ## xAI xAI reasoning models (Grok) support native thinking. To preserve the thinking content for multi-turn conversations, enable [`XaiModelSettings.xai_include_encrypted_content`](/docs/ai/api/models/xai/#pydantic_ai.models.xai.XaiModelSettings.xai_include_encrypted_content). xai\_thinking\_part.py ```python from pydantic_ai import Agent from pydantic_ai.models.xai import XaiModel, XaiModelSettings model = XaiModel('grok-4-fast-reasoning') settings = XaiModelSettings(xai_include_encrypted_content=True) agent = Agent(model, model_settings=settings) ... ``` ## Bedrock Although Bedrock Converse doesn't provide a unified API to enable thinking, you can still use [`BedrockModelSettings.bedrock_additional_model_requests_fields`](/docs/ai/api/models/bedrock/#pydantic_ai.models.bedrock.BedrockModelSettings.bedrock_additional_model_requests_fields) [model setting](/docs/ai/core-concepts/agent#model-run-settings) to pass provider-specific configuration: - [Claude](#tab-panel-0) - [OpenAI](#tab-panel-1) - [Qwen](#tab-panel-2) - [Deepseek](#tab-panel-3) bedrock\_claude\_thinking\_part.py ```python from pydantic_ai import Agent from pydantic_ai.models.bedrock import BedrockConverseModel, BedrockModelSettings model = BedrockConverseModel('us.anthropic.claude-sonnet-4-5-20250929-v1:0') model_settings = BedrockModelSettings( bedrock_additional_model_requests_fields={ 'thinking': {'type': 'enabled', 'budget_tokens': 1024} } ) agent = Agent(model=model, model_settings=model_settings) ``` bedrock\_openai\_thinking\_part.py ```python from pydantic_ai import Agent from pydantic_ai.models.bedrock import BedrockConverseModel, BedrockModelSettings model = BedrockConverseModel('openai.gpt-oss-120b-1:0') model_settings = BedrockModelSettings( bedrock_additional_model_requests_fields={'reasoning_effort': 'low'} ) agent = Agent(model=model, model_settings=model_settings) ``` bedrock\_qwen\_thinking\_part.py ```python from pydantic_ai import Agent from pydantic_ai.models.bedrock import BedrockConverseModel, BedrockModelSettings model = BedrockConverseModel('qwen.qwen3-32b-v1:0') model_settings = BedrockModelSettings( bedrock_additional_model_requests_fields={'reasoning_config': 'high'} ) agent = Agent(model=model, model_settings=model_settings) ``` Reasoning is [always enabled](https://docs.aws.amazon.com/bedrock/latest/userguide/inference-reasoning.html) for Deepseek model bedrock\_deepseek\_thinking\_part.py ```python from pydantic_ai import Agent from pydantic_ai.models.bedrock import BedrockConverseModel model = BedrockConverseModel('us.deepseek.r1-v1:0') agent = Agent(model=model) ``` ## Groq Groq supports different formats to receive thinking parts: - `"raw"`: The thinking part is included in the text content inside `` tags, which are automatically converted to [`ThinkingPart`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.ThinkingPart) objects. - `"hidden"`: The thinking part is not included in the text content. - `"parsed"`: The thinking part has its own structured part in the response which is converted into a [`ThinkingPart`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.ThinkingPart) object. To enable thinking, use the [`GroqModelSettings.groq_reasoning_format`](/docs/ai/api/models/groq/#pydantic_ai.models.groq.GroqModelSettings.groq_reasoning_format) [model setting](/docs/ai/core-concepts/agent#model-run-settings): groq\_thinking\_part.py ```python from pydantic_ai import Agent from pydantic_ai.models.groq import GroqModel, GroqModelSettings model = GroqModel('qwen/qwen3-32b') settings = GroqModelSettings(groq_reasoning_format='parsed') agent = Agent(model, model_settings=settings) ... ``` Note Groq does not support truly disabling thinking. When `thinking=False` is set via the unified setting, Pydantic AI sends `reasoning_format='hidden'`, which suppresses reasoning output but the model may still reason internally. ## OpenRouter To enable thinking, use the [`OpenRouterModelSettings.openrouter_reasoning`](/docs/ai/api/models/openrouter/#pydantic_ai.models.openrouter.OpenRouterModelSettings.openrouter_reasoning) [model setting](/docs/ai/core-concepts/agent#model-run-settings). openrouter\_thinking\_part.py ```python from pydantic_ai import Agent from pydantic_ai.models.openrouter import OpenRouterModel, OpenRouterModelSettings model = OpenRouterModel('openai/gpt-5.2') settings = OpenRouterModelSettings(openrouter_reasoning={'effort': 'high'}) agent = Agent(model, model_settings=settings) ... ``` ## Mistral Thinking is supported by the `magistral` family of models. It does not need to be specifically enabled. ## Cohere Thinking is supported by the `command-a-reasoning-08-2025` model. It does not need to be specifically enabled. ## Hugging Face Text output inside `` tags is automatically converted to [`ThinkingPart`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.ThinkingPart) objects. You can customize the tags using the [`thinking_tags`](/docs/ai/api/pydantic-ai/profiles/#pydantic_ai.profiles.ModelProfile.thinking_tags) field on the [model profile](/docs/ai/models/openai#model-profile). ## Outlines Some local models run through Outlines include in their text output a thinking part delimited by tags. In that case, it will be handled by Pydantic AI that will separate the thinking part from the final answer without the need to specifically enable it. The thinking tags used by default are `""` and `""`. If your model uses different tags, you can specify them in the [model profile](/docs/ai/models/openai#model-profile) using the [`thinking_tags`](/docs/ai/api/pydantic-ai/profiles/#pydantic_ai.profiles.ModelProfile.thinking_tags) field. Outlines currently does not support thinking along with structured output. If you provide an `output_type`, the model text output will not contain a thinking part with the associated tags, and you may experience degraded performance. --- # [](https://pydantic.dev/docs/ai/api/builtin_tools/) --- # [fasta2a](https://pydantic.dev/docs/ai/api/fasta2a/) # fasta2a ### Storage **Bases:** `ABC`, `Generic[ContextT]` A storage to retrieve and save tasks, as well as retrieve and save context. The storage serves two purposes: 1. Task storage: Stores tasks in A2A protocol format with their status, artifacts, and message history 2. Context storage: Stores conversation context in a format optimized for the specific agent implementation #### Methods ##### load\_task `@abstractmethod` `@async` ```python def load_task(task_id: str, history_length: int | None = None) -> Task | None ``` Load a task from storage. If the task is not found, return None. ###### Returns `Task` | [`None`](https://docs.python.org/3/library/constants.html#None) ##### submit\_task `@abstractmethod` `@async` ```python def submit_task(context_id: str, message: Message) -> Task ``` Submit a task to storage. ###### Returns `Task` ##### update\_task `@abstractmethod` `@async` ```python def update_task( task_id: str, state: TaskState, new_artifacts: list[Artifact] | None = None, new_messages: list[Message] | None = None, ) -> Task ``` Update the state of a task. Appends artifacts and messages, if specified. ###### Returns `Task` ##### load\_context `@abstractmethod` `@async` ```python def load_context(context_id: str) -> ContextT | None ``` Retrieve the stored context given the `context_id`. ###### Returns `ContextT` | [`None`](https://docs.python.org/3/library/constants.html#None) ##### update\_context `@abstractmethod` `@async` ```python def update_context(context_id: str, context: ContextT) -> None ``` Updates the context for a `context_id`. Implementing agent can decide what to store in context. ###### Returns [`None`](https://docs.python.org/3/library/constants.html#None) ### Broker **Bases:** `ABC` The broker class is in charge of scheduling the tasks. The HTTP server uses the broker to schedule tasks. The simple implementation is the `InMemoryBroker`, which is the broker that runs the tasks in the same process as the HTTP server. That said, this class can be extended to support remote workers. #### Methods ##### run\_task `@abstractmethod` `@async` ```python def run_task(params: TaskSendParams) -> None ``` Send a task to be executed by the worker. ###### Returns [`None`](https://docs.python.org/3/library/constants.html#None) ##### cancel\_task `@abstractmethod` `@async` ```python def cancel_task(params: TaskIdParams) -> None ``` Cancel a task. ###### Returns [`None`](https://docs.python.org/3/library/constants.html#None) ##### receive\_task\_operations `@abstractmethod` ```python def receive_task_operations() -> AsyncIterator[TaskOperation] ``` Receive task operations from the broker. On a multi-worker setup, the broker will need to round-robin the task operations between the workers. ###### Returns [`AsyncIterator`](https://docs.python.org/3/library/typing.html#typing.AsyncIterator)\[`TaskOperation`\] ### Worker **Bases:** `ABC`, `Generic[ContextT]` A worker is responsible for executing tasks. #### Methods ##### run `@async` ```python def run() -> AsyncIterator[None] ``` Run the worker. It connects to the broker, and it makes itself available to receive commands. ###### Returns [`AsyncIterator`](https://docs.python.org/3/library/typing.html#typing.AsyncIterator)\[[`None`](https://docs.python.org/3/library/constants.html#None)\] ### FastA2A **Bases:** `Starlette` The main class for the FastA2A library. ### Skill **Bases:** [`TypedDict`](https://docs.python.org/3/library/typing.html#typing.TypedDict) Skills are a unit of capability that an agent can perform. #### Attributes ##### id A unique identifier for the skill. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### name Human readable name of the skill. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### description A human-readable description of the skill. It will be used by the client or a human as a hint to understand the skill. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### tags Set of tag-words describing classes of capabilities for this specific skill. Examples: "cooking", "customer support", "billing". **Type:** [`list`](https://docs.python.org/3/glossary.html#term-list)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str)\] ##### examples The set of example scenarios that the skill can perform. Will be used by the client as a hint to understand how the skill can be used. (e.g. "I need a recipe for bread") **Type:** [`NotRequired`](https://docs.python.org/3/library/typing.html#typing.NotRequired)\[[`list`](https://docs.python.org/3/glossary.html#term-list)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str)\]\] ##### input\_modes Supported mime types for input data. **Type:** [`list`](https://docs.python.org/3/glossary.html#term-list)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str)\] ##### output\_modes Supported mime types for output data. **Type:** [`list`](https://docs.python.org/3/glossary.html#term-list)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str)\] This module contains the schema for the agent card. ### AgentCard **Bases:** [`TypedDict`](https://docs.python.org/3/library/typing.html#typing.TypedDict) The card that describes an agent. #### Attributes ##### name Human readable name of the agent e.g. "Recipe Agent". **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### description A human-readable description of the agent. Used to assist users and other agents in understanding what the agent can do. (e.g. "Agent that helps users with recipes and cooking.") **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### url A URL to the address the agent is hosted at. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### version The version of the agent - format is up to the provider. (e.g. "1.0.0") **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### protocol\_version The version of the A2A protocol this agent supports. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### provider The service provider of the agent. **Type:** [`NotRequired`](https://docs.python.org/3/library/typing.html#typing.NotRequired)\[`AgentProvider`\] ##### documentation\_url A URL to documentation for the agent. **Type:** [`NotRequired`](https://docs.python.org/3/library/typing.html#typing.NotRequired)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str)\] ##### icon\_url A URL to an icon for the agent. **Type:** [`NotRequired`](https://docs.python.org/3/library/typing.html#typing.NotRequired)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str)\] ##### preferred\_transport The transport of the preferred endpoint. If empty, defaults to JSONRPC. **Type:** [`NotRequired`](https://docs.python.org/3/library/typing.html#typing.NotRequired)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str)\] ##### additional\_interfaces Announcement of additional supported transports. **Type:** [`NotRequired`](https://docs.python.org/3/library/typing.html#typing.NotRequired)\[[`list`](https://docs.python.org/3/glossary.html#term-list)\[`AgentInterface`\]\] ##### capabilities The capabilities of the agent. **Type:** `AgentCapabilities` ##### security Security requirements for contacting the agent. **Type:** [`NotRequired`](https://docs.python.org/3/library/typing.html#typing.NotRequired)\[[`list`](https://docs.python.org/3/glossary.html#term-list)\[[`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), [`list`](https://docs.python.org/3/glossary.html#term-list)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str)\]\]\]\] ##### security\_schemes Security scheme definitions. **Type:** [`NotRequired`](https://docs.python.org/3/library/typing.html#typing.NotRequired)\[[`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), `SecurityScheme`\]\] ##### default\_input\_modes Supported mime types for input data. **Type:** [`list`](https://docs.python.org/3/glossary.html#term-list)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str)\] ##### default\_output\_modes Supported mime types for output data. **Type:** [`list`](https://docs.python.org/3/glossary.html#term-list)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str)\] ##### skills The set of skills, or distinct capabilities, that the agent can perform. **Type:** [`list`](https://docs.python.org/3/glossary.html#term-list)\[`Skill`\] ### AgentProvider **Bases:** [`TypedDict`](https://docs.python.org/3/library/typing.html#typing.TypedDict) The service provider of the agent. #### Attributes ##### organization The name of the agent provider's organization. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### url A URL for the agent provider's website or relevant documentation. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ### AgentCapabilities **Bases:** [`TypedDict`](https://docs.python.org/3/library/typing.html#typing.TypedDict) The capabilities of the agent. #### Attributes ##### streaming Whether the agent supports streaming. **Type:** [`NotRequired`](https://docs.python.org/3/library/typing.html#typing.NotRequired)\[[`bool`](https://docs.python.org/3/library/functions.html#bool)\] ##### push\_notifications Whether the agent can notify updates to client. **Type:** [`NotRequired`](https://docs.python.org/3/library/typing.html#typing.NotRequired)\[[`bool`](https://docs.python.org/3/library/functions.html#bool)\] ##### state\_transition\_history Whether the agent exposes status change history for tasks. **Type:** [`NotRequired`](https://docs.python.org/3/library/typing.html#typing.NotRequired)\[[`bool`](https://docs.python.org/3/library/functions.html#bool)\] ### HttpSecurityScheme **Bases:** [`TypedDict`](https://docs.python.org/3/library/typing.html#typing.TypedDict) HTTP security scheme. #### Attributes ##### type The type of the security scheme. Must be 'http'. **Type:** [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['http'\] ##### scheme The name of the HTTP Authorization scheme. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### bearer\_format A hint to the client to identify how the bearer token is formatted. **Type:** [`NotRequired`](https://docs.python.org/3/library/typing.html#typing.NotRequired)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str)\] ##### description Description of this security scheme. **Type:** [`NotRequired`](https://docs.python.org/3/library/typing.html#typing.NotRequired)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str)\] ### ApiKeySecurityScheme **Bases:** [`TypedDict`](https://docs.python.org/3/library/typing.html#typing.TypedDict) API Key security scheme. #### Attributes ##### type The type of the security scheme. Must be 'apiKey'. **Type:** [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['apiKey'\] ##### name The name of the header, query or cookie parameter to be used. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### in\_ The location of the API key. **Type:** [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['query', 'header', 'cookie'\] ##### description Description of this security scheme. **Type:** [`NotRequired`](https://docs.python.org/3/library/typing.html#typing.NotRequired)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str)\] ### OAuth2SecurityScheme **Bases:** [`TypedDict`](https://docs.python.org/3/library/typing.html#typing.TypedDict) OAuth2 security scheme. #### Attributes ##### type The type of the security scheme. Must be 'oauth2'. **Type:** [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['oauth2'\] ##### flows An object containing configuration information for the flow types supported. **Type:** [`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] ##### description Description of this security scheme. **Type:** [`NotRequired`](https://docs.python.org/3/library/typing.html#typing.NotRequired)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str)\] ### OpenIdConnectSecurityScheme **Bases:** [`TypedDict`](https://docs.python.org/3/library/typing.html#typing.TypedDict) OpenID Connect security scheme. #### Attributes ##### type The type of the security scheme. Must be 'openIdConnect'. **Type:** [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['openIdConnect'\] ##### open\_id\_connect\_url OpenId Connect URL to discover OAuth2 configuration values. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### description Description of this security scheme. **Type:** [`NotRequired`](https://docs.python.org/3/library/typing.html#typing.NotRequired)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str)\] ### AgentInterface **Bases:** [`TypedDict`](https://docs.python.org/3/library/typing.html#typing.TypedDict) An interface that the agent supports. #### Attributes ##### transport The transport protocol (e.g., 'jsonrpc', 'websocket'). **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### url The URL endpoint for this transport. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### description Description of this interface. **Type:** [`NotRequired`](https://docs.python.org/3/library/typing.html#typing.NotRequired)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str)\] ### AgentExtension **Bases:** [`TypedDict`](https://docs.python.org/3/library/typing.html#typing.TypedDict) A declaration of an extension supported by an Agent. #### Attributes ##### uri The URI of the extension. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### description A description of how this agent uses this extension. **Type:** [`NotRequired`](https://docs.python.org/3/library/typing.html#typing.NotRequired)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str)\] ##### required Whether the client must follow specific requirements of the extension. **Type:** [`NotRequired`](https://docs.python.org/3/library/typing.html#typing.NotRequired)\[[`bool`](https://docs.python.org/3/library/functions.html#bool)\] ##### params Optional configuration for the extension. **Type:** [`NotRequired`](https://docs.python.org/3/library/typing.html#typing.NotRequired)\[[`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\]\] ### Skill **Bases:** [`TypedDict`](https://docs.python.org/3/library/typing.html#typing.TypedDict) Skills are a unit of capability that an agent can perform. #### Attributes ##### id A unique identifier for the skill. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### name Human readable name of the skill. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### description A human-readable description of the skill. It will be used by the client or a human as a hint to understand the skill. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### tags Set of tag-words describing classes of capabilities for this specific skill. Examples: "cooking", "customer support", "billing". **Type:** [`list`](https://docs.python.org/3/glossary.html#term-list)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str)\] ##### examples The set of example scenarios that the skill can perform. Will be used by the client as a hint to understand how the skill can be used. (e.g. "I need a recipe for bread") **Type:** [`NotRequired`](https://docs.python.org/3/library/typing.html#typing.NotRequired)\[[`list`](https://docs.python.org/3/glossary.html#term-list)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str)\]\] ##### input\_modes Supported mime types for input data. **Type:** [`list`](https://docs.python.org/3/glossary.html#term-list)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str)\] ##### output\_modes Supported mime types for output data. **Type:** [`list`](https://docs.python.org/3/glossary.html#term-list)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str)\] ### Artifact **Bases:** [`TypedDict`](https://docs.python.org/3/library/typing.html#typing.TypedDict) Agents generate Artifacts as an end result of a Task. Artifacts are immutable, can be named, and can have multiple parts. A streaming response can append parts to existing Artifacts. A single Task can generate many Artifacts. For example, "create a webpage" could create separate HTML and image Artifacts. #### Attributes ##### artifact\_id Unique identifier for the artifact. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### name The name of the artifact. **Type:** [`NotRequired`](https://docs.python.org/3/library/typing.html#typing.NotRequired)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str)\] ##### description A description of the artifact. **Type:** [`NotRequired`](https://docs.python.org/3/library/typing.html#typing.NotRequired)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str)\] ##### parts The parts that make up the artifact. **Type:** [`list`](https://docs.python.org/3/glossary.html#term-list)\[`Part`\] ##### metadata Metadata about the artifact. **Type:** [`NotRequired`](https://docs.python.org/3/library/typing.html#typing.NotRequired)\[[`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\]\] ##### extensions Array of extensions. **Type:** [`NotRequired`](https://docs.python.org/3/library/typing.html#typing.NotRequired)\[[`list`](https://docs.python.org/3/glossary.html#term-list)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str)\]\] ##### append Whether to append this artifact to an existing one. **Type:** [`NotRequired`](https://docs.python.org/3/library/typing.html#typing.NotRequired)\[[`bool`](https://docs.python.org/3/library/functions.html#bool)\] ##### last\_chunk Whether this is the last chunk of the artifact. **Type:** [`NotRequired`](https://docs.python.org/3/library/typing.html#typing.NotRequired)\[[`bool`](https://docs.python.org/3/library/functions.html#bool)\] ### PushNotificationConfig **Bases:** [`TypedDict`](https://docs.python.org/3/library/typing.html#typing.TypedDict) Configuration for push notifications. A2A supports a secure notification mechanism whereby an agent can notify a client of an update outside a connected session via a PushNotificationService. Within and across enterprises, it is critical that the agent verifies the identity of the notification service, authenticates itself with the service, and presents an identifier that ties the notification to the executing Task. The target server of the PushNotificationService should be considered a separate service, and is not guaranteed (or even expected) to be the client directly. This PushNotificationService is responsible for authenticating and authorizing the agent and for proxying the verified notification to the appropriate endpoint (which could be anything from a pub/sub queue, to an email inbox or other service, etc.). For contrived scenarios with isolated client-agent pairs (e.g. local service mesh in a contained VPC, etc.) or isolated environments without enterprise security concerns, the client may choose to simply open a port and act as its own PushNotificationService. Any enterprise implementation will likely have a centralized service that authenticates the remote agents with trusted notification credentials and can handle online/offline scenarios. (This should be thought of similarly to a mobile Push Notification Service). #### Attributes ##### id Server-assigned identifier. **Type:** [`NotRequired`](https://docs.python.org/3/library/typing.html#typing.NotRequired)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str)\] ##### url The URL to send push notifications to. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### token Token unique to this task/session. **Type:** [`NotRequired`](https://docs.python.org/3/library/typing.html#typing.NotRequired)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str)\] ##### authentication Authentication details for push notifications. **Type:** [`NotRequired`](https://docs.python.org/3/library/typing.html#typing.NotRequired)\[`SecurityScheme`\] ### TaskPushNotificationConfig **Bases:** [`TypedDict`](https://docs.python.org/3/library/typing.html#typing.TypedDict) Configuration for task push notifications. #### Attributes ##### id The task id. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### push\_notification\_config The push notification configuration. **Type:** `PushNotificationConfig` ### Message **Bases:** [`TypedDict`](https://docs.python.org/3/library/typing.html#typing.TypedDict) A Message contains any content that is not an Artifact. This can include things like agent thoughts, user context, instructions, errors, status, or metadata. All content from a client comes in the form of a Message. Agents send Messages to communicate status or to provide instructions (whereas generated results are sent as Artifacts). A Message can have multiple parts to denote different pieces of content. For example, a user request could include a textual description from a user and then multiple files used as context from the client. #### Attributes ##### role The role of the message. **Type:** [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['user', 'agent'\] ##### parts The parts of the message. **Type:** [`list`](https://docs.python.org/3/glossary.html#term-list)\[`Part`\] ##### kind Event type. **Type:** [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['message'\] ##### metadata Metadata about the message. **Type:** [`NotRequired`](https://docs.python.org/3/library/typing.html#typing.NotRequired)\[[`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\]\] ##### message\_id Identifier created by the message creator. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### context\_id The context the message is associated with. **Type:** [`NotRequired`](https://docs.python.org/3/library/typing.html#typing.NotRequired)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str)\] ##### task\_id Identifier of task the message is related to. **Type:** [`NotRequired`](https://docs.python.org/3/library/typing.html#typing.NotRequired)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str)\] ##### reference\_task\_ids Array of task IDs this message references. **Type:** [`NotRequired`](https://docs.python.org/3/library/typing.html#typing.NotRequired)\[[`list`](https://docs.python.org/3/glossary.html#term-list)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str)\]\] ##### extensions Array of extensions. **Type:** [`NotRequired`](https://docs.python.org/3/library/typing.html#typing.NotRequired)\[[`list`](https://docs.python.org/3/glossary.html#term-list)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str)\]\] ### TextPart **Bases:** `_BasePart` A part that contains text. #### Attributes ##### kind The kind of the part. **Type:** [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['text'\] ##### text The text of the part. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ### FileWithBytes **Bases:** [`TypedDict`](https://docs.python.org/3/library/typing.html#typing.TypedDict) File with base64 encoded data. #### Attributes ##### bytes The base64 encoded content of the file. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### mime\_type Optional mime type for the file. **Type:** [`NotRequired`](https://docs.python.org/3/library/typing.html#typing.NotRequired)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str)\] ### FileWithUri **Bases:** [`TypedDict`](https://docs.python.org/3/library/typing.html#typing.TypedDict) File with URI reference. #### Attributes ##### uri The URI of the file. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### mime\_type The mime type of the file. **Type:** [`NotRequired`](https://docs.python.org/3/library/typing.html#typing.NotRequired)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str)\] ### FilePart **Bases:** `_BasePart` A part that contains a file. #### Attributes ##### kind The kind of the part. **Type:** [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['file'\] ##### file The file content - either bytes or URI. **Type:** `FileWithBytes` | `FileWithUri` ### DataPart **Bases:** `_BasePart` A part that contains structured data. #### Attributes ##### kind The kind of the part. **Type:** [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['data'\] ##### data The data of the part. **Type:** [`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] ### TaskStatus **Bases:** [`TypedDict`](https://docs.python.org/3/library/typing.html#typing.TypedDict) Status and accompanying message for a task. #### Attributes ##### state The current state of the task. **Type:** `TaskState` ##### message Additional status updates for client. **Type:** [`NotRequired`](https://docs.python.org/3/library/typing.html#typing.NotRequired)\[`Message`\] ##### timestamp ISO datetime value of when the status was updated. **Type:** [`NotRequired`](https://docs.python.org/3/library/typing.html#typing.NotRequired)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str)\] ### Task **Bases:** [`TypedDict`](https://docs.python.org/3/library/typing.html#typing.TypedDict) A Task is a stateful entity that allows Clients and Remote Agents to achieve a specific outcome. Clients and Remote Agents exchange Messages within a Task. Remote Agents generate results as Artifacts. A Task is always created by a Client and the status is always determined by the Remote Agent. #### Attributes ##### id Unique identifier for the task. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### context\_id The context the task is associated with. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### kind Event type. **Type:** [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['task'\] ##### status Current status of the task. **Type:** `TaskStatus` ##### history Optional history of messages. **Type:** [`NotRequired`](https://docs.python.org/3/library/typing.html#typing.NotRequired)\[[`list`](https://docs.python.org/3/glossary.html#term-list)\[`Message`\]\] ##### artifacts Collection of artifacts created by the agent. **Type:** [`NotRequired`](https://docs.python.org/3/library/typing.html#typing.NotRequired)\[[`list`](https://docs.python.org/3/glossary.html#term-list)\[`Artifact`\]\] ##### metadata Extension metadata. **Type:** [`NotRequired`](https://docs.python.org/3/library/typing.html#typing.NotRequired)\[[`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\]\] ### TaskStatusUpdateEvent **Bases:** [`TypedDict`](https://docs.python.org/3/library/typing.html#typing.TypedDict) Sent by server during message/stream requests. #### Attributes ##### task\_id The id of the task. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### context\_id The context the task is associated with. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### kind Event type. **Type:** [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['status-update'\] ##### status The status of the task. **Type:** `TaskStatus` ##### final Indicates the end of the event stream. **Type:** [`bool`](https://docs.python.org/3/library/functions.html#bool) ##### metadata Extension metadata. **Type:** [`NotRequired`](https://docs.python.org/3/library/typing.html#typing.NotRequired)\[[`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\]\] ### TaskArtifactUpdateEvent **Bases:** [`TypedDict`](https://docs.python.org/3/library/typing.html#typing.TypedDict) Sent by server during message/stream requests. #### Attributes ##### task\_id The id of the task. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### context\_id The context the task is associated with. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### kind Event type identification. **Type:** [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['artifact-update'\] ##### artifact The artifact that was updated. **Type:** `Artifact` ##### append Whether to append to existing artifact (true) or replace (false). **Type:** [`NotRequired`](https://docs.python.org/3/library/typing.html#typing.NotRequired)\[[`bool`](https://docs.python.org/3/library/functions.html#bool)\] ##### last\_chunk Indicates this is the final chunk of the artifact. **Type:** [`NotRequired`](https://docs.python.org/3/library/typing.html#typing.NotRequired)\[[`bool`](https://docs.python.org/3/library/functions.html#bool)\] ##### metadata Extension metadata. **Type:** [`NotRequired`](https://docs.python.org/3/library/typing.html#typing.NotRequired)\[[`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\]\] ### TaskIdParams **Bases:** [`TypedDict`](https://docs.python.org/3/library/typing.html#typing.TypedDict) Parameters for a task id. #### Attributes ##### id The unique identifier for the task. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### metadata Optional metadata associated with the request. **Type:** [`NotRequired`](https://docs.python.org/3/library/typing.html#typing.NotRequired)\[[`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\]\] ### TaskQueryParams **Bases:** `TaskIdParams` Query parameters for a task. #### Attributes ##### history\_length Number of recent messages to be retrieved. **Type:** [`NotRequired`](https://docs.python.org/3/library/typing.html#typing.NotRequired)\[[`int`](https://docs.python.org/3/library/functions.html#int)\] ### MessageSendConfiguration **Bases:** [`TypedDict`](https://docs.python.org/3/library/typing.html#typing.TypedDict) Configuration for the send message request. #### Attributes ##### accepted\_output\_modes Accepted output modalities by the client. **Type:** [`list`](https://docs.python.org/3/glossary.html#term-list)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str)\] ##### blocking If the server should treat the client as a blocking request. **Type:** [`NotRequired`](https://docs.python.org/3/library/typing.html#typing.NotRequired)\[[`bool`](https://docs.python.org/3/library/functions.html#bool)\] ##### history\_length Number of recent messages to be retrieved. **Type:** [`NotRequired`](https://docs.python.org/3/library/typing.html#typing.NotRequired)\[[`int`](https://docs.python.org/3/library/functions.html#int)\] ##### push\_notification\_config Where the server should send notifications when disconnected. **Type:** [`NotRequired`](https://docs.python.org/3/library/typing.html#typing.NotRequired)\[`PushNotificationConfig`\] ### MessageSendParams **Bases:** [`TypedDict`](https://docs.python.org/3/library/typing.html#typing.TypedDict) Parameters for message/send method. #### Attributes ##### configuration Send message configuration. **Type:** [`NotRequired`](https://docs.python.org/3/library/typing.html#typing.NotRequired)\[`MessageSendConfiguration`\] ##### message The message being sent to the server. **Type:** `Message` ##### metadata Extension metadata. **Type:** [`NotRequired`](https://docs.python.org/3/library/typing.html#typing.NotRequired)\[[`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\]\] ### TaskSendParams **Bases:** [`TypedDict`](https://docs.python.org/3/library/typing.html#typing.TypedDict) Internal parameters for task execution within the framework. Note: This is not part of the A2A protocol - it's used internally for broker/worker communication. #### Attributes ##### id The id of the task. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### context\_id The context id for the task. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### message The message to process. **Type:** `Message` ##### history\_length Number of recent messages to be retrieved. **Type:** [`NotRequired`](https://docs.python.org/3/library/typing.html#typing.NotRequired)\[[`int`](https://docs.python.org/3/library/functions.html#int)\] ##### metadata Extension metadata. **Type:** [`NotRequired`](https://docs.python.org/3/library/typing.html#typing.NotRequired)\[[`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\]\] ### ListTaskPushNotificationConfigParams **Bases:** [`TypedDict`](https://docs.python.org/3/library/typing.html#typing.TypedDict) Parameters for getting list of pushNotificationConfigurations associated with a Task. #### Attributes ##### id Task id. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### metadata Extension metadata. **Type:** [`NotRequired`](https://docs.python.org/3/library/typing.html#typing.NotRequired)\[[`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\]\] ### DeleteTaskPushNotificationConfigParams **Bases:** [`TypedDict`](https://docs.python.org/3/library/typing.html#typing.TypedDict) Parameters for removing pushNotificationConfiguration associated with a Task. #### Attributes ##### id Task id. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### push\_notification\_config\_id The push notification config id to delete. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### metadata Extension metadata. **Type:** [`NotRequired`](https://docs.python.org/3/library/typing.html#typing.NotRequired)\[[`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\]\] ### JSONRPCMessage **Bases:** [`TypedDict`](https://docs.python.org/3/library/typing.html#typing.TypedDict) A JSON RPC message. #### Attributes ##### jsonrpc The JSON RPC version. **Type:** [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['2.0'\] ##### id The request id. **Type:** [`int`](https://docs.python.org/3/library/functions.html#int) | [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) ### JSONRPCRequest **Bases:** `JSONRPCMessage`, `Generic[Method, Params]` A JSON RPC request. #### Attributes ##### method The method to call. **Type:** `Method` ##### params The parameters to pass to the method. **Type:** `Params` ### JSONRPCError **Bases:** [`TypedDict`](https://docs.python.org/3/library/typing.html#typing.TypedDict), `Generic[CodeT, MessageT]` A JSON RPC error. #### Attributes ##### code A number that indicates the error type that occurred. **Type:** `CodeT` ##### message A string providing a short description of the error. **Type:** `MessageT` ##### data A primitive or structured value containing additional information about the error. **Type:** [`NotRequired`](https://docs.python.org/3/library/typing.html#typing.NotRequired)\[[`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] ### JSONRPCResponse **Bases:** `JSONRPCMessage`, `Generic[ResultT, ErrorT]` A JSON RPC response. ### SecurityScheme A security scheme for authentication. **Default:** `Annotated[Union[HttpSecurityScheme, ApiKeySecurityScheme, OAuth2SecurityScheme, OpenIdConnectSecurityScheme], pydantic.Field(discriminator='type')]` ### Part A fully formed piece of content exchanged between a client and a remote agent as part of a Message or an Artifact. Each Part has its own content type and metadata. **Default:** `Annotated[Union[TextPart, FilePart, DataPart], pydantic.Field(discriminator='kind')]` ### TaskState The possible states of a task. **Type:** [`TypeAlias`](https://docs.python.org/3/library/typing.html#typing.TypeAlias) **Default:** `Literal['submitted', 'working', 'input-required', 'completed', 'canceled', 'failed', 'rejected', 'auth-required', 'unknown']` ### JSONParseError A JSON RPC error for a parse error. **Default:** `JSONRPCError[Literal[-32700], Literal['Invalid JSON payload']]` ### InvalidRequestError A JSON RPC error for an invalid request. **Default:** `JSONRPCError[Literal[-32600], Literal['Request payload validation error']]` ### MethodNotFoundError A JSON RPC error for a method not found. **Default:** `JSONRPCError[Literal[-32601], Literal['Method not found']]` ### InvalidParamsError A JSON RPC error for invalid parameters. **Default:** `JSONRPCError[Literal[-32602], Literal['Invalid parameters']]` ### InternalError A JSON RPC error for an internal error. **Default:** `JSONRPCError[Literal[-32603], Literal['Internal error']]` ### TaskNotFoundError A JSON RPC error for a task not found. **Default:** `JSONRPCError[Literal[-32001], Literal['Task not found']]` ### TaskNotCancelableError A JSON RPC error for a task not cancelable. **Default:** `JSONRPCError[Literal[-32002], Literal['Task not cancelable']]` ### PushNotificationNotSupportedError A JSON RPC error for a push notification not supported. **Default:** `JSONRPCError[Literal[-32003], Literal['Push notification not supported']]` ### UnsupportedOperationError A JSON RPC error for an unsupported operation. **Default:** `JSONRPCError[Literal[-32004], Literal['This operation is not supported']]` ### ContentTypeNotSupportedError A JSON RPC error for incompatible content types. **Default:** `JSONRPCError[Literal[-32005], Literal['Incompatible content types']]` ### InvalidAgentResponseError A JSON RPC error for invalid agent response. **Default:** `JSONRPCError[Literal[-32006], Literal['Invalid agent response']]` ### SendMessageRequest A JSON RPC request to send a message. **Default:** `JSONRPCRequest[Literal['message/send'], MessageSendParams]` ### SendMessageResponse A JSON RPC response to send a message. **Default:** `JSONRPCResponse[Union[Task, Message], JSONRPCError[Any, Any]]` ### StreamMessageRequest A JSON RPC request to stream a message. **Default:** `JSONRPCRequest[Literal['message/stream'], MessageSendParams]` ### StreamMessageResponse A JSON RPC response to a StreamMessageRequest. **Default:** `JSONRPCResponse[Union[Task, Message, TaskStatusUpdateEvent, TaskArtifactUpdateEvent], JSONRPCError[Any, Any]]` ### GetTaskRequest A JSON RPC request to get a task. **Default:** `JSONRPCRequest[Literal['tasks/get'], TaskQueryParams]` ### GetTaskResponse A JSON RPC response to get a task. **Default:** `JSONRPCResponse[Task, TaskNotFoundError]` ### CancelTaskRequest A JSON RPC request to cancel a task. **Default:** `JSONRPCRequest[Literal['tasks/cancel'], TaskIdParams]` ### CancelTaskResponse A JSON RPC response to cancel a task. **Default:** `JSONRPCResponse[Task, Union[TaskNotCancelableError, TaskNotFoundError]]` ### SetTaskPushNotificationRequest A JSON RPC request to set a task push notification. **Default:** `JSONRPCRequest[Literal['tasks/pushNotification/set'], TaskPushNotificationConfig]` ### SetTaskPushNotificationResponse A JSON RPC response to set a task push notification. **Default:** `JSONRPCResponse[TaskPushNotificationConfig, PushNotificationNotSupportedError]` ### GetTaskPushNotificationRequest A JSON RPC request to get a task push notification. **Default:** `JSONRPCRequest[Literal['tasks/pushNotification/get'], TaskIdParams]` ### GetTaskPushNotificationResponse A JSON RPC response to get a task push notification. **Default:** `JSONRPCResponse[TaskPushNotificationConfig, PushNotificationNotSupportedError]` ### ResubscribeTaskRequest A JSON RPC request to resubscribe to a task. **Default:** `JSONRPCRequest[Literal['tasks/resubscribe'], TaskIdParams]` ### ListTaskPushNotificationConfigRequest A JSON RPC request to list task push notification configs. **Default:** `JSONRPCRequest[Literal['tasks/pushNotificationConfig/list'], ListTaskPushNotificationConfigParams]` ### DeleteTaskPushNotificationConfigRequest A JSON RPC request to delete a task push notification config. **Default:** `JSONRPCRequest[Literal['tasks/pushNotificationConfig/delete'], DeleteTaskPushNotificationConfigParams]` ### A2ARequest A JSON RPC request to the A2A server. **Default:** `Annotated[Union[SendMessageRequest, StreamMessageRequest, GetTaskRequest, CancelTaskRequest, SetTaskPushNotificationRequest, GetTaskPushNotificationRequest, ResubscribeTaskRequest, ListTaskPushNotificationConfigRequest, DeleteTaskPushNotificationConfigRequest], Discriminator('method')]` ### A2AResponse A JSON RPC response from the A2A server. **Type:** [`TypeAlias`](https://docs.python.org/3/library/typing.html#typing.TypeAlias) **Default:** `Union[SendMessageResponse, StreamMessageResponse, GetTaskResponse, CancelTaskResponse, SetTaskPushNotificationResponse, GetTaskPushNotificationResponse]` ### A2AClient A client for the A2A protocol. #### Methods ##### send\_message `@async` ```python def send_message( message: Message, metadata: dict[str, Any] | None = None, configuration: MessageSendConfiguration | None = None, ) -> SendMessageResponse ``` Send a message using the A2A protocol. Returns a JSON-RPC response containing either a result (Task) or an error. ###### Returns `SendMessageResponse` ### UnexpectedResponseError **Bases:** [`Exception`](https://docs.python.org/3/library/exceptions.html#Exception) An error raised when an unexpected response is received from the server. --- # [pydantic_ai.models.anthropic](https://pydantic.dev/docs/ai/api/models/anthropic/) # pydantic\_ai.models.anthropic ## Setup For details on how to set up authentication with this model, see [model configuration for Anthropic](/docs/ai/models/anthropic). ### AnthropicModelSettings **Bases:** [`ModelSettings`](/docs/ai/api/pydantic-ai/settings/#pydantic_ai.settings.ModelSettings) Settings used for an Anthropic model request. #### Attributes ##### anthropic\_metadata An object describing metadata about the request. Contains `user_id`, an external identifier for the user who is associated with the request. **Type:** `BetaMetadataParam` ##### anthropic\_thinking Determine whether the model should generate a thinking block. See [the Anthropic docs](https://docs.anthropic.com/en/docs/build-with-claude/extended-thinking) for more information. **Type:** `BetaThinkingConfigParam` ##### anthropic\_cache\_tool\_definitions Whether to add `cache_control` to the last tool definition. When enabled, the last tool in the `tools` array will have `cache_control` set, allowing Anthropic to cache tool definitions and reduce costs. If `True`, uses TTL='5m'. You can also specify '5m' or '1h' directly. See [https://docs.anthropic.com/en/docs/build-with-claude/prompt-caching](https://docs.anthropic.com/en/docs/build-with-claude/prompt-caching) for more information. **Type:** [`bool`](https://docs.python.org/3/library/functions.html#bool) | [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['5m', '1h'\] ##### anthropic\_service\_tier The service tier to use for the model request. See [https://docs.anthropic.com/en/docs/build-with-claude/latency-and-throughput](https://docs.anthropic.com/en/docs/build-with-claude/latency-and-throughput) for more information. **Type:** [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['auto', 'standard\_only'\] ##### anthropic\_cache\_instructions Whether to add `cache_control` to the last system prompt block. When enabled, the last system prompt will have `cache_control` set, allowing Anthropic to cache system instructions and reduce costs. If `True`, uses TTL='5m'. You can also specify '5m' or '1h' directly. See [https://docs.anthropic.com/en/docs/build-with-claude/prompt-caching](https://docs.anthropic.com/en/docs/build-with-claude/prompt-caching) for more information. **Type:** [`bool`](https://docs.python.org/3/library/functions.html#bool) | [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['5m', '1h'\] ##### anthropic\_cache\_messages Whether to add `cache_control` to the last message content block. This is an alternative to `anthropic_cache` for Anthropic-compatible gateways and proxies that accept the Anthropic message format but don't support the top-level automatic caching parameter. If `True`, uses TTL='5m'. You can also specify '5m' or '1h' directly. Cannot be combined with `anthropic_cache`. **Type:** [`bool`](https://docs.python.org/3/library/functions.html#bool) | [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['5m', '1h'\] ##### anthropic\_cache Enable prompt caching for multi-turn conversations. Passes a top-level `cache_control` parameter so the server automatically applies a cache breakpoint to the last cacheable block and moves it forward as conversations grow. On Bedrock and Vertex, automatic caching is not yet supported, so this falls back to per-block caching on the last user message. If the last content block already has `cache_control` from an explicit `CachePoint`, it is preserved. If `True`, uses TTL='5m'. You can also specify '5m' or '1h' directly. This can be combined with explicit cache breakpoints (`anthropic_cache_instructions`, `anthropic_cache_tool_definitions`, `CachePoint`). The automatic breakpoint counts as 1 of Anthropic's 4 cache point slots; we automatically trim excess explicit breakpoints. See [https://docs.anthropic.com/en/docs/build-with-claude/prompt-caching#automatic-caching](https://docs.anthropic.com/en/docs/build-with-claude/prompt-caching#automatic-caching) for more information. **Type:** [`bool`](https://docs.python.org/3/library/functions.html#bool) | [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['5m', '1h'\] ##### anthropic\_effort The effort level for the model to use when generating a response. See [the Anthropic docs](https://docs.anthropic.com/en/docs/build-with-claude/effort) for more information. **Type:** [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['low', 'medium', 'high', 'xhigh', 'max'\] | [`None`](https://docs.python.org/3/library/constants.html#None) ##### anthropic\_task\_budget Task budget configuration for Claude Opus 4.7 beta requests. Maps to `output_config.task_budget`. This setting is currently only supported on `claude-opus-4-7`, and Pydantic AI automatically enables Anthropic's required task-budget beta when it is present. Omit `remaining` unless you are intentionally carrying a budget across compaction or other rewritten context. **Type:** `AnthropicTaskBudget` ##### anthropic\_container Container configuration for multi-turn conversations. By default, if previous messages contain a container\_id (from a prior response), it will be reused automatically. Set to `False` to force a fresh container (ignore any `container_id` from history). Set to a container id string (e.g. `'container_xxx'`) to explicitly reuse a container, or to a `BetaContainerParams` dict (e.g. `{'skills': [...]}` or `{'id': 'container_xxx', 'skills': [...]}`) when passing Skills to the Anthropic Skills beta. **Type:** `BetaContainerParams` | [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\[[`False`](https://docs.python.org/3/library/constants.html#False)\] ##### anthropic\_code\_execution\_tool\_version Which Anthropic code execution tool version to send for `CodeExecutionTool`. Defaults to `'auto'`, which uses the default version from the model profile: `'20260120'` for Sonnet 4.5+ and Opus 4.5+, otherwise `'20250825'`. Set a concrete version to force that tool version; a `UserError` is raised if the selected model profile does not support that version. **Type:** `AnthropicCodeExecutionToolVersion` | [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['auto'\] ##### anthropic\_eager\_input\_streaming Whether to enable eager input streaming on tool definitions. When enabled, all tool definitions will have `eager_input_streaming` set to `True`, allowing Anthropic to stream tool call arguments incrementally instead of buffering the entire JSON before streaming. This reduces latency for tool calls with large inputs. See [https://platform.claude.com/docs/en/agents-and-tools/tool-use/fine-grained-tool-streaming](https://platform.claude.com/docs/en/agents-and-tools/tool-use/fine-grained-tool-streaming) for more information. **Type:** [`bool`](https://docs.python.org/3/library/functions.html#bool) ##### anthropic\_betas List of Anthropic beta features to enable for API requests. Each item can be a known beta name (e.g. 'interleaved-thinking-2025-05-14') or a custom string. Merged with auto-added betas (e.g. builtin tools) and any betas from extra\_headers\['anthropic-beta'\]. See the Anthropic docs for available beta features. **Type:** [`list`](https://docs.python.org/3/glossary.html#term-list)\[`AnthropicBetaParam`\] ##### anthropic\_speed The inference speed mode for this request. `'fast'` enables high output-tokens-per-second inference for supported models (currently Claude Opus 4.6 only). On unsupported models or clients, `anthropic_speed='fast'` is ignored with a `UserWarning`. Fast mode is a research preview and only available on the direct Anthropic API (not Bedrock, Vertex, or Foundry); see [the Anthropic docs](https://platform.claude.com/docs/en/build-with-claude/fast-mode) for details. Note: switching between `'fast'` and `'standard'` invalidates the prompt cache. **Type:** [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['standard', 'fast'\] ##### anthropic\_context\_management Context management configuration for automatic compaction. When configured, Anthropic will automatically compact older context when the input token count exceeds the configured threshold. The compaction produces a summary that replaces the compacted messages. See [the Anthropic docs](https://docs.anthropic.com/en/docs/build-with-claude/compaction) for more details. **Type:** `BetaContextManagementConfigParam` ### AnthropicModel **Bases:** `Model[AsyncAnthropicClient]` A model that uses the Anthropic API. Internally, this uses the [Anthropic Python client](https://github.com/anthropics/anthropic-sdk-python) to interact with the API. Apart from `__init__`, all methods are private or match those of the base class. #### Attributes ##### model\_name The model name. **Type:** `AnthropicModelName` ##### system The model provider. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) #### Methods ##### \_\_init\_\_ ```python def __init__( model_name: AnthropicModelName, provider: Literal['anthropic', 'gateway'] | Provider[AsyncAnthropicClient] = 'anthropic', profile: ModelProfileSpec | None = None, settings: ModelSettings | None = None, ) ``` Initialize an Anthropic model. ###### Parameters **`model_name`** : `AnthropicModelName` The name of the Anthropic model to use. List of model names available [here](https://docs.anthropic.com/en/docs/about-claude/models). **`provider`** : [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['anthropic', 'gateway'\] | `Provider`\[`AsyncAnthropicClient`\] _Default:_ `'anthropic'` The provider to use for the Anthropic API. Can be either the string 'anthropic' or an instance of `Provider[AsyncAnthropicClient]`. Defaults to 'anthropic'. **`profile`** : `ModelProfileSpec` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` The model profile to use. Defaults to a profile picked by the provider based on the model name. The default 'anthropic' provider will use the default `..profiles.anthropic.anthropic_model_profile`. **`settings`** : [`ModelSettings`](/docs/ai/api/pydantic-ai/settings/#pydantic_ai.settings.ModelSettings) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Default model settings for this model instance. ##### supported\_native\_tools `@classmethod` ```python def supported_native_tools(cls) -> frozenset[type[AbstractNativeTool]] ``` The set of builtin tool types this model can handle. ###### Returns [`frozenset`](https://docs.python.org/3/library/stdtypes.html#frozenset)\[[`type`](https://docs.python.org/3/glossary.html#term-type)\[`AbstractNativeTool`\]\] ### AnthropicCompaction **Bases:** `AbstractCapability[AgentDepsT]` Compaction capability for Anthropic models. Configures automatic context management via Anthropic's `context_management` API parameter. Compaction triggers server-side when input tokens exceed the configured threshold. Example usage: ```python from pydantic_ai import Agent from pydantic_ai.models.anthropic import AnthropicCompaction agent = Agent( 'anthropic:claude-sonnet-4-6', capabilities=[AnthropicCompaction(token_threshold=100_000)], ) ``` #### Methods ##### \_\_init\_\_ ```python def __init__( token_threshold: int = 150000, instructions: str | None = None, pause_after_compaction: bool = False, ) -> None ``` Initialize the Anthropic compaction capability. ###### Returns [`None`](https://docs.python.org/3/library/constants.html#None) ###### Parameters **`token_threshold`** : [`int`](https://docs.python.org/3/library/functions.html#int) _Default:_ `150000` Compact when input tokens exceed this threshold. Minimum 50,000. **`instructions`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Custom instructions for the compaction summarization. **`pause_after_compaction`** : [`bool`](https://docs.python.org/3/library/functions.html#bool) _Default:_ `False` If `True`, the response will stop after the compaction block with `stop_reason='compaction'`, allowing explicit handling. ### AnthropicStreamedResponse **Bases:** `StreamedResponse` Implementation of `StreamedResponse` for Anthropic models. #### Attributes ##### model\_name Get the model name of the response. **Type:** `AnthropicModelName` ##### provider\_name Get the provider name. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### provider\_url Get the provider base URL. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### timestamp Get the timestamp of the response. **Type:** [`datetime`](https://docs.python.org/3/library/datetime.html#module-datetime) ### LatestAnthropicModelNames Anthropic model names from the installed SDK. **Default:** `ModelParam` ### AnthropicModelName Possible Anthropic model names. The installed Anthropic SDK exposes the current literal set and still allows arbitrary string model names. See [the Anthropic docs](https://docs.anthropic.com/en/docs/about-claude/models) for a full list. **Default:** `LatestAnthropicModelNames` ### AnthropicTaskBudget Anthropic task budget payload for `output_config.task_budget`. **Type:** [`TypeAlias`](https://docs.python.org/3/library/typing.html#typing.TypeAlias) **Default:** `BetaTokenTaskBudgetParam` --- # [pydantic_ai.models](https://pydantic.dev/docs/ai/api/models/base/) # pydantic\_ai.models Logic related to making requests to an LLM. The aim here is to make a common interface for different LLMs, so that the rest of the code can be agnostic to the specific LLM being used. ### ModelRequestParameters Configuration for an agent's request to a model, specifically related to tools and output handling. #### Attributes ##### instruction\_parts Structured instruction parts with metadata about their origin (static vs dynamic). Static instructions (`dynamic=False`) come from literal strings passed to `Agent(instructions=...)`. Dynamic instructions (`dynamic=True`) come from `@agent.instructions` functions, `TemplateStr`, or toolset `get_instructions()` methods. Models that support granular caching (e.g. Anthropic, Bedrock) use this to place cache boundaries at the static/dynamic instruction boundary. **Type:** [`list`](https://docs.python.org/3/glossary.html#term-list)\[[`InstructionPart`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.InstructionPart)\] | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `None` ##### thinking Resolved thinking/reasoning configuration for this request. `None` means the model should use its default behavior. Set by the base `Model.prepare_request()` from the unified `thinking` field in `ModelSettings`, after checking that the model's profile supports thinking. **Type:** `ThinkingLevel` | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `None` ##### builtin\_tools Deprecated: use `native_tools` instead. **Type:** [`list`](https://docs.python.org/3/glossary.html#term-list)\[`AbstractNativeTool`\] #### Methods ##### with\_default\_output\_mode ```python def with_default_output_mode( output_mode: StructuredOutputMode, ) -> ModelRequestParameters ``` Set the default output mode if the current mode is 'auto', atomically updating allow\_text\_output. No-op if the current output\_mode is not 'auto'. This ensures the two fields stay in sync -- output\_mode='tool' implies allow\_text\_output=False, while 'native' and 'prompted' imply allow\_text\_output=True. ###### Returns `ModelRequestParameters` ### Model **Bases:** `ABC`, `Generic[InterfaceClient]` Abstract class for a model. #### Attributes ##### provider The provider for this model, if any. **Type:** `Provider`\[`InterfaceClient`\] | [`None`](https://docs.python.org/3/library/constants.html#None) ##### settings Get the model settings. **Type:** [`ModelSettings`](/docs/ai/api/pydantic-ai/settings/#pydantic_ai.settings.ModelSettings) | [`None`](https://docs.python.org/3/library/constants.html#None) ##### model\_name The model name. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### model\_id The fully qualified model name in `'provider:model_name'` format. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### label Human-friendly display label for the model. Handles common patterns: - gpt-5 -> GPT 5 - claude-sonnet-4-5 -> Claude Sonnet 4.5 - gemini-2.5-pro -> Gemini 2.5 Pro - meta-llama/llama-3-70b -> Llama 3 70b (OpenRouter style) **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### profile The model profile. We use this to compute the intersection of the profile's supported\_native\_tools and the model's implemented tools, ensuring model.profile.supported\_native\_tools is the single source of truth for what native tools are actually usable. **Type:** [`ModelProfile`](/docs/ai/api/pydantic-ai/profiles/#pydantic_ai.profiles.ModelProfile) ##### system The model provider, ex: openai. Use to populate the `gen_ai.system` OpenTelemetry semantic convention attribute, so should use well-known values listed in [https://opentelemetry.io/docs/specs/semconv/attributes-registry/gen-ai/#gen-ai-system](https://opentelemetry.io/docs/specs/semconv/attributes-registry/gen-ai/#gen-ai-system) when applicable. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### base\_url The base URL for the provider API, if available. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) #### Methods ##### \_\_init\_\_ ```python def __init__( settings: ModelSettings | None = None, profile: ModelProfileSpec | None = None, ) -> None ``` Initialize the model with optional settings and profile. ###### Returns [`None`](https://docs.python.org/3/library/constants.html#None) ###### Parameters **`settings`** : [`ModelSettings`](/docs/ai/api/pydantic-ai/settings/#pydantic_ai.settings.ModelSettings) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Model-specific settings that will be used as defaults for this model. **`profile`** : `ModelProfileSpec` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` The model profile to use. ##### \_\_aenter\_\_ `@async` ```python def __aenter__() -> Self ``` Enter the model context, delegating to the provider to manage its HTTP client lifecycle. ###### Returns [`Self`](https://docs.python.org/3/library/typing.html#typing.Self) ##### \_\_aexit\_\_ `@async` ```python def __aexit__( exc_type: type[BaseException] | None, exc_val: BaseException | None, exc_tb: TracebackType | None, ) -> bool | None ``` Exit the model context, closing the provider's HTTP client if it owns one. ###### Returns [`bool`](https://docs.python.org/3/library/functions.html#bool) | [`None`](https://docs.python.org/3/library/constants.html#None) ##### request `@abstractmethod` `@async` ```python def request( messages: list[ModelMessage], model_settings: ModelSettings | None, model_request_parameters: ModelRequestParameters, ) -> ModelResponse ``` Make a request to the model. This is ultimately called by `pydantic_ai._agent_graph.ModelRequestNode._make_request(...)`. ###### Returns [`ModelResponse`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.ModelResponse) ##### count\_tokens `@async` ```python def count_tokens( messages: list[ModelMessage], model_settings: ModelSettings | None, model_request_parameters: ModelRequestParameters, ) -> RequestUsage ``` Make a request to the model for counting tokens. ###### Returns [`RequestUsage`](/docs/ai/api/pydantic-ai/usage/#pydantic_ai.usage.RequestUsage) ##### compact\_messages `@async` ```python def compact_messages( request_context: ModelRequestContext, instructions: str | None = None, ) -> ModelResponse ``` Compact messages to reduce conversation context size. This method is optional and only supported by specific providers (e.g. OpenAI Responses API). Providers that support compaction override this method with their implementation. ###### Returns [`ModelResponse`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.ModelResponse) ##### request\_stream `@async` ```python def request_stream( messages: list[ModelMessage], model_settings: ModelSettings | None, model_request_parameters: ModelRequestParameters, run_context: RunContext[Any] | None = None, ) -> AsyncIterator[StreamedResponse] ``` Make a request to the model and return a streaming response. ###### Returns [`AsyncIterator`](https://docs.python.org/3/library/typing.html#typing.AsyncIterator)\[`StreamedResponse`\] ##### customize\_request\_parameters ```python def customize_request_parameters( model_request_parameters: ModelRequestParameters, ) -> ModelRequestParameters ``` Customize the request parameters for the model. This method can be overridden by subclasses to modify the request parameters before sending them to the model. In particular, this method can be used to make modifications to the generated tool JSON schemas if necessary for vendor/model-specific reasons. ###### Returns `ModelRequestParameters` ##### prepare\_request ```python def prepare_request( model_settings: ModelSettings | None, model_request_parameters: ModelRequestParameters, ) -> tuple[ModelSettings | None, ModelRequestParameters] ``` Prepare request inputs before they are passed to the provider. This merges the given `model_settings` with the model's own `settings` attribute and ensures `customize_request_parameters` is applied to the resolved [`ModelRequestParameters`](/docs/ai/api/models/base/#pydantic_ai.models.ModelRequestParameters). Subclasses can override this method if they need to customize the preparation flow further, but most implementations should simply call `self.prepare_request(...)` at the start of their `request` (and related) methods. ###### Returns [`tuple`](https://docs.python.org/3/library/stdtypes.html#tuple)\[[`ModelSettings`](/docs/ai/api/pydantic-ai/settings/#pydantic_ai.settings.ModelSettings) | [`None`](https://docs.python.org/3/library/constants.html#None), `ModelRequestParameters`\] ##### prepare\_messages ```python def prepare_messages(messages: list[ModelMessage]) -> list[ModelMessage] ``` Pre-process the message history before it's handed to the adapter's message-prep step. Currently translates any typed `NativeToolSearch*Part` instances carried over from a prior native turn (e.g. Anthropic / OpenAI Responses) into the local-shape `ToolSearch*Part` instances when the active model's profile doesn't support `ToolSearchTool` -- splitting the single `ModelResponse(call+return)` carrying the inline server-side result into `ModelResponse(call) + ModelRequest(return)` so the adapter sees a normal function-call exchange against `search_tools`. Subclasses normally don't need to override this; the framework calls it on the agent's behalf in `_agent_graph._make_request` so per-adapter message-prep code sees a homogeneous shape regardless of which provider produced the prior turn. ###### Returns [`list`](https://docs.python.org/3/glossary.html#term-list)\[[`ModelMessage`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.ModelMessage)\] ##### supported\_native\_tools `@classmethod` ```python def supported_native_tools(cls) -> frozenset[type[AbstractNativeTool]] ``` Return the set of native tool types this model class can handle. Subclasses should override this to reflect their actual capabilities. Default is empty set - subclasses must explicitly declare support. ###### Returns [`frozenset`](https://docs.python.org/3/library/stdtypes.html#frozenset)\[[`type`](https://docs.python.org/3/glossary.html#term-type)\[`AbstractNativeTool`\]\] ##### supported\_builtin\_tools `@classmethod` ```python def supported_builtin_tools(cls) -> frozenset[type[AbstractNativeTool]] ``` Deprecated: use [`supported_native_tools`](/docs/ai/api/models/base/#pydantic_ai.models.Model.supported_native_tools) instead. ###### Returns [`frozenset`](https://docs.python.org/3/library/stdtypes.html#frozenset)\[[`type`](https://docs.python.org/3/glossary.html#term-type)\[`AbstractNativeTool`\]\] ### StreamedResponse **Bases:** `ABC` Streamed response from an LLM when calling a tool. #### Attributes ##### model\_name Get the model name of the response. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### provider\_name Get the provider name. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) ##### provider\_url Get the provider base URL. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) ##### timestamp Get the timestamp of the response. **Type:** [`datetime`](https://docs.python.org/3/library/datetime.html#module-datetime) ##### cancelled Whether the stream has been cancelled via `cancel()`. **Type:** [`bool`](https://docs.python.org/3/library/functions.html#bool) #### Methods ##### \_\_aiter\_\_ ```python def __aiter__() -> AsyncIterator[ModelResponseStreamEvent] ``` Stream the response as an async iterable of [`ModelResponseStreamEvent`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.ModelResponseStreamEvent)s. This proxies the `_event_iterator()` and emits all events, while also checking for matches on the result schema and emitting a [`FinalResultEvent`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.FinalResultEvent) if/when the first match is found. ###### Returns [`AsyncIterator`](https://docs.python.org/3/library/typing.html#typing.AsyncIterator)\[[`ModelResponseStreamEvent`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.ModelResponseStreamEvent)\] ##### cancel `@async` ```python def cancel() -> None ``` Cancel the stream, stopping token generation. Sets `self._cancelled = True` before delegating to `close_stream()` so the flag is visible to any iterator that observes the transport error raised when the underlying connection is torn down, even if `close_stream()` itself raises. ###### Returns [`None`](https://docs.python.org/3/library/constants.html#None) ##### get\_stream\_cancel\_errors ```python def get_stream_cancel_errors() -> tuple[type[BaseException], ...] ``` Return transport errors caused by `cancel()` tearing down the stream. The default covers model classes whose SDKs iterate `httpx` responses directly (Anthropic, OpenAI, Groq, Mistral, Google GenAI, HuggingFace, and the custom Gemini client), since they let bare `httpx` errors propagate from chunk reads. Model classes that use other transports (for example gRPC or botocore) should override this method. ###### Returns [`tuple`](https://docs.python.org/3/library/stdtypes.html#tuple)\[[`type`](https://docs.python.org/3/glossary.html#term-type)\[[`BaseException`](https://docs.python.org/3/library/exceptions.html#BaseException)\], ...\] ##### close\_stream `@async` ```python def close_stream() -> None ``` Close the underlying HTTP/gRPC connection. Model classes must override this to stop token generation (and billing) on the remote side. Integrations that cannot support cancellation should leave the default implementation so `cancel()` fails clearly rather than silently reporting successful cancellation while generation continues. ###### Returns [`None`](https://docs.python.org/3/library/constants.html#None) ##### get ```python def get() -> ModelResponse ``` Build a [`ModelResponse`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.ModelResponse) from the data received from the stream so far. ###### Returns [`ModelResponse`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.ModelResponse) ##### usage ```python def usage() -> RequestUsage ``` Get the usage of the response so far. This will not be the final usage until the stream is exhausted. ###### Returns [`RequestUsage`](/docs/ai/api/pydantic-ai/usage/#pydantic_ai.usage.RequestUsage) ### check\_allow\_model\_requests ```python def check_allow_model_requests() -> None ``` Check if model requests are allowed. If you're defining your own models that have costs or latency associated with their use, you should call this in [`Model.request`](/docs/ai/api/models/base/#pydantic_ai.models.Model.request) and [`Model.request_stream`](/docs/ai/api/models/base/#pydantic_ai.models.Model.request_stream). #### Returns [`None`](https://docs.python.org/3/library/constants.html#None) #### Raises - `RuntimeError` -- If model requests are not allowed. ### override\_allow\_model\_requests ```python def override_allow_model_requests(allow_model_requests: bool) -> Iterator[None] ``` Context manager to temporarily override [`ALLOW_MODEL_REQUESTS`](/docs/ai/api/models/base/#pydantic_ai.models.ALLOW_MODEL_REQUESTS). #### Returns [`Iterator`](https://docs.python.org/3/library/typing.html#typing.Iterator)\[[`None`](https://docs.python.org/3/library/constants.html#None)\] #### Parameters **`allow_model_requests`** : [`bool`](https://docs.python.org/3/library/functions.html#bool) Whether to allow model requests within the context. ### KnownModelName Known model names that can be used with the `model` parameter of [`Agent`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.Agent). `KnownModelName` is provided as a concise way to specify a model. **Default:** `TypeAliasType('KnownModelName', Literal['anthropic:claude-3-haiku-20240307', 'anthropic:claude-haiku-4-5-20251001', 'anthropic:claude-mythos-preview', 'anthropic:claude-haiku-4-5', 'anthropic:claude-opus-4-0', 'anthropic:claude-opus-4-1', 'anthropic:claude-opus-4-1-20250805', 'anthropic:claude-opus-4-20250514', 'anthropic:claude-opus-4-5-20251101', 'anthropic:claude-opus-4-5', 'anthropic:claude-opus-4-6', 'anthropic:claude-opus-4-7', 'anthropic:claude-sonnet-4-0', 'anthropic:claude-sonnet-4-20250514', 'anthropic:claude-sonnet-4-5-20250929', 'anthropic:claude-sonnet-4-5', 'anthropic:claude-sonnet-4-6', 'bedrock:amazon.titan-text-express-v1', 'bedrock:amazon.titan-text-lite-v1', 'bedrock:amazon.titan-tg1-large', 'bedrock:anthropic.claude-3-5-haiku-20241022-v1:0', 'bedrock:anthropic.claude-3-5-sonnet-20240620-v1:0', 'bedrock:anthropic.claude-3-5-sonnet-20241022-v2:0', 'bedrock:anthropic.claude-3-7-sonnet-20250219-v1:0', 'bedrock:anthropic.claude-3-haiku-20240307-v1:0', 'bedrock:anthropic.claude-3-opus-20240229-v1:0', 'bedrock:anthropic.claude-3-sonnet-20240229-v1:0', 'bedrock:anthropic.claude-haiku-4-5-20251001-v1:0', 'bedrock:anthropic.claude-instant-v1', 'bedrock:anthropic.claude-opus-4-20250514-v1:0', 'bedrock:anthropic.claude-sonnet-4-20250514-v1:0', 'bedrock:anthropic.claude-sonnet-4-5-20250929-v1:0', 'bedrock:anthropic.claude-sonnet-4-6', 'bedrock:anthropic.claude-v2:1', 'bedrock:anthropic.claude-v2', 'bedrock:cohere.command-light-text-v14', 'bedrock:cohere.command-r-plus-v1:0', 'bedrock:cohere.command-r-v1:0', 'bedrock:cohere.command-text-v14', 'bedrock:eu.anthropic.claude-haiku-4-5-20251001-v1:0', 'bedrock:eu.anthropic.claude-sonnet-4-20250514-v1:0', 'bedrock:eu.anthropic.claude-sonnet-4-5-20250929-v1:0', 'bedrock:eu.anthropic.claude-sonnet-4-6', 'bedrock:global.anthropic.claude-opus-4-5-20251101-v1:0', 'bedrock:meta.llama3-1-405b-instruct-v1:0', 'bedrock:meta.llama3-1-70b-instruct-v1:0', 'bedrock:meta.llama3-1-8b-instruct-v1:0', 'bedrock:meta.llama3-70b-instruct-v1:0', 'bedrock:meta.llama3-8b-instruct-v1:0', 'bedrock:mistral.mistral-7b-instruct-v0:2', 'bedrock:mistral.mistral-large-2402-v1:0', 'bedrock:mistral.mistral-large-2407-v1:0', 'bedrock:mistral.mixtral-8x7b-instruct-v0:1', 'bedrock:us.amazon.nova-2-lite-v1:0', 'bedrock:us.amazon.nova-lite-v1:0', 'bedrock:us.amazon.nova-micro-v1:0', 'bedrock:us.amazon.nova-pro-v1:0', 'bedrock:us.anthropic.claude-3-5-haiku-20241022-v1:0', 'bedrock:us.anthropic.claude-3-5-sonnet-20240620-v1:0', 'bedrock:us.anthropic.claude-3-5-sonnet-20241022-v2:0', 'bedrock:us.anthropic.claude-3-7-sonnet-20250219-v1:0', 'bedrock:us.anthropic.claude-3-haiku-20240307-v1:0', 'bedrock:us.anthropic.claude-3-opus-20240229-v1:0', 'bedrock:us.anthropic.claude-3-sonnet-20240229-v1:0', 'bedrock:us.anthropic.claude-haiku-4-5-20251001-v1:0', 'bedrock:us.anthropic.claude-opus-4-20250514-v1:0', 'bedrock:us.anthropic.claude-sonnet-4-20250514-v1:0', 'bedrock:us.anthropic.claude-sonnet-4-5-20250929-v1:0', 'bedrock:us.anthropic.claude-sonnet-4-6', 'bedrock:us.meta.llama3-1-70b-instruct-v1:0', 'bedrock:us.meta.llama3-1-8b-instruct-v1:0', 'bedrock:us.meta.llama3-2-11b-instruct-v1:0', 'bedrock:us.meta.llama3-2-1b-instruct-v1:0', 'bedrock:us.meta.llama3-2-3b-instruct-v1:0', 'bedrock:us.meta.llama3-2-90b-instruct-v1:0', 'bedrock:us.meta.llama3-3-70b-instruct-v1:0', 'cerebras:gpt-oss-120b', 'cerebras:llama3.1-8b', 'cerebras:qwen-3-235b-a22b-instruct-2507', 'cerebras:zai-glm-4.7', 'cohere:c4ai-aya-expanse-32b', 'cohere:c4ai-aya-expanse-8b', 'cohere:command-nightly', 'cohere:command-r-08-2024', 'cohere:command-r-plus-08-2024', 'cohere:command-r7b-12-2024', 'deepseek:deepseek-chat', 'deepseek:deepseek-reasoner', 'deepseek:deepseek-v4-flash', 'deepseek:deepseek-v4-pro', 'gateway/anthropic:claude-3-haiku-20240307', 'gateway/anthropic:claude-haiku-4-5-20251001', 'gateway/anthropic:claude-mythos-preview', 'gateway/anthropic:claude-haiku-4-5', 'gateway/anthropic:claude-opus-4-0', 'gateway/anthropic:claude-opus-4-1', 'gateway/anthropic:claude-opus-4-1-20250805', 'gateway/anthropic:claude-opus-4-20250514', 'gateway/anthropic:claude-opus-4-5-20251101', 'gateway/anthropic:claude-opus-4-5', 'gateway/anthropic:claude-opus-4-6', 'gateway/anthropic:claude-opus-4-7', 'gateway/anthropic:claude-sonnet-4-0', 'gateway/anthropic:claude-sonnet-4-20250514', 'gateway/anthropic:claude-sonnet-4-5-20250929', 'gateway/anthropic:claude-sonnet-4-5', 'gateway/anthropic:claude-sonnet-4-6', 'gateway/bedrock:anthropic.claude-3-5-sonnet-20240620-v1:0', 'gateway/bedrock:anthropic.claude-3-haiku-20240307-v1:0', 'gateway/bedrock:eu.anthropic.claude-haiku-4-5-20251001-v1:0', 'gateway/bedrock:eu.anthropic.claude-sonnet-4-20250514-v1:0', 'gateway/bedrock:eu.anthropic.claude-sonnet-4-5-20250929-v1:0', 'gateway/bedrock:eu.anthropic.claude-sonnet-4-6', 'gateway/bedrock:global.anthropic.claude-opus-4-5-20251101-v1:0', 'gateway/google-cloud:gemini-2.5-flash-image', 'gateway/google-cloud:gemini-2.5-flash-lite-preview-09-2025', 'gateway/google-cloud:gemini-2.5-flash-lite', 'gateway/google-cloud:gemini-2.5-flash', 'gateway/google-cloud:gemini-2.5-pro', 'gateway/google-cloud:gemini-3-flash-preview', 'gateway/google-cloud:gemini-3-pro-image-preview', 'gateway/google-cloud:gemini-3.1-flash-image-preview', 'gateway/google-cloud:gemini-3.1-flash-lite-preview', 'gateway/google-cloud:gemini-3.1-pro-preview', 'gateway/groq:llama-3.1-8b-instant', 'gateway/groq:llama-3.3-70b-versatile', 'gateway/groq:meta-llama/llama-4-scout-17b-16e-instruct', 'gateway/groq:moonshotai/kimi-k2-instruct-0905', 'gateway/groq:openai/gpt-oss-120b', 'gateway/groq:openai/gpt-oss-20b', 'gateway/groq:openai/gpt-oss-safeguard-20b', 'gateway/openai:gpt-3.5-turbo-0125', 'gateway/openai:gpt-3.5-turbo-1106', 'gateway/openai:gpt-3.5-turbo-16k', 'gateway/openai:gpt-3.5-turbo', 'gateway/openai:gpt-4-0613', 'gateway/openai:gpt-4-turbo-2024-04-09', 'gateway/openai:gpt-4-turbo', 'gateway/openai:gpt-4.1-2025-04-14', 'gateway/openai:gpt-4.1-mini-2025-04-14', 'gateway/openai:gpt-4.1-mini', 'gateway/openai:gpt-4.1-nano-2025-04-14', 'gateway/openai:gpt-4.1-nano', 'gateway/openai:gpt-4.1', 'gateway/openai:gpt-4', 'gateway/openai:gpt-4o-2024-05-13', 'gateway/openai:gpt-4o-2024-08-06', 'gateway/openai:gpt-4o-2024-11-20', 'gateway/openai:gpt-4o-mini-2024-07-18', 'gateway/openai:gpt-4o-mini-search-preview-2025-03-11', 'gateway/openai:gpt-4o-mini-search-preview', 'gateway/openai:gpt-4o-mini', 'gateway/openai:gpt-4o-search-preview-2025-03-11', 'gateway/openai:gpt-4o-search-preview', 'gateway/openai:gpt-4o', 'gateway/openai:gpt-5-2025-08-07', 'gateway/openai:gpt-5-chat-latest', 'gateway/openai:gpt-5-mini-2025-08-07', 'gateway/openai:gpt-5-mini', 'gateway/openai:gpt-5-nano-2025-08-07', 'gateway/openai:gpt-5-nano', 'gateway/openai:gpt-5.1-2025-11-13', 'gateway/openai:gpt-5.1-chat-latest', 'gateway/openai:gpt-5.1', 'gateway/openai:gpt-5.2-2025-12-11', 'gateway/openai:gpt-5.2-chat-latest', 'gateway/openai:gpt-5.2', 'gateway/openai:gpt-5.4-mini-2026-03-17', 'gateway/openai:gpt-5.4-mini', 'gateway/openai:gpt-5.4-nano-2026-03-17', 'gateway/openai:gpt-5.4-nano', 'gateway/openai:gpt-5.4', 'gateway/openai:gpt-5', 'gateway/openai:o1-2024-12-17', 'gateway/openai:o1', 'gateway/openai:o3-2025-04-16', 'gateway/openai:o3-mini-2025-01-31', 'gateway/openai:o3-mini', 'gateway/openai:o3', 'gateway/openai:o4-mini-2025-04-16', 'gateway/openai:o4-mini', 'google-cloud:gemini-2.0-flash-lite', 'google-cloud:gemini-2.0-flash', 'google-cloud:gemini-2.5-flash-image', 'google-cloud:gemini-2.5-flash-lite-preview-09-2025', 'google-cloud:gemini-2.5-flash-lite', 'google-cloud:gemini-2.5-flash-preview-09-2025', 'google-cloud:gemini-2.5-flash', 'google-cloud:gemini-2.5-pro', 'google-cloud:gemini-3-flash-preview', 'google-cloud:gemini-3-pro-image-preview', 'google-cloud:gemini-3-pro-preview', 'google-cloud:gemini-3.1-flash-image-preview', 'google-cloud:gemini-3.1-flash-lite-preview', 'google-cloud:gemini-3.1-pro-preview', 'google-cloud:gemini-flash-latest', 'google-cloud:gemini-flash-lite-latest', 'google:gemini-2.0-flash-lite', 'google:gemini-2.0-flash', 'google:gemini-2.5-flash-image', 'google:gemini-2.5-flash-lite-preview-09-2025', 'google:gemini-2.5-flash-lite', 'google:gemini-2.5-flash-preview-09-2025', 'google:gemini-2.5-flash', 'google:gemini-2.5-pro', 'google:gemini-3-flash-preview', 'google:gemini-3-pro-image-preview', 'google:gemini-3-pro-preview', 'google:gemini-3.1-flash-image-preview', 'google:gemini-3.1-flash-lite-preview', 'google:gemini-3.1-pro-preview', 'google:gemini-flash-latest', 'google:gemini-flash-lite-latest', 'grok:grok-2-image-1212', 'grok:grok-2-vision-1212', 'grok:grok-3-fast', 'grok:grok-3-mini-fast', 'grok:grok-3-mini', 'grok:grok-3', 'grok:grok-4-0709', 'grok:grok-4-latest', 'grok:grok-4-1-fast-non-reasoning', 'grok:grok-4-1-fast-reasoning', 'grok:grok-4-1-fast', 'grok:grok-4-fast-non-reasoning', 'grok:grok-4-fast-reasoning', 'grok:grok-4-fast', 'grok:grok-4', 'grok:grok-code-fast-1', 'xai:grok-3', 'xai:grok-3-fast', 'xai:grok-3-fast-latest', 'xai:grok-3-latest', 'xai:grok-3-mini', 'xai:grok-3-mini-fast', 'xai:grok-3-mini-fast-latest', 'xai:grok-4', 'xai:grok-4-0709', 'xai:grok-4-1-fast', 'xai:grok-4-1-fast-non-reasoning', 'xai:grok-4-1-fast-non-reasoning-latest', 'xai:grok-4-1-fast-reasoning', 'xai:grok-4-1-fast-reasoning-latest', 'xai:grok-4-fast', 'xai:grok-4-fast-non-reasoning', 'xai:grok-4-fast-non-reasoning-latest', 'xai:grok-4-fast-reasoning', 'xai:grok-4-fast-reasoning-latest', 'xai:grok-4-latest', 'xai:grok-code-fast-1', 'groq:llama-3.1-8b-instant', 'groq:llama-3.3-70b-versatile', 'groq:meta-llama/llama-guard-4-12b', 'groq:openai/gpt-oss-120b', 'groq:openai/gpt-oss-20b', 'groq:whisper-large-v3', 'groq:whisper-large-v3-turbo', 'groq:meta-llama/llama-4-maverick-17b-128e-instruct', 'groq:meta-llama/llama-4-scout-17b-16e-instruct', 'groq:meta-llama/llama-prompt-guard-2-22m', 'groq:meta-llama/llama-prompt-guard-2-86m', 'groq:moonshotai/kimi-k2-instruct-0905', 'groq:openai/gpt-oss-safeguard-20b', 'groq:playai-tts', 'groq:playai-tts-arabic', 'groq:qwen/qwen-3-32b', 'heroku:claude-3-5-haiku', 'heroku:claude-3-5-sonnet-latest', 'heroku:claude-3-7-sonnet', 'heroku:claude-3-haiku', 'heroku:claude-4-5-haiku', 'heroku:claude-4-5-sonnet', 'heroku:claude-4-6-sonnet', 'heroku:claude-4-sonnet', 'heroku:claude-opus-4-5', 'heroku:claude-opus-4-6', 'heroku:deepseek-v3-2', 'heroku:glm-4-7', 'heroku:glm-4-7-flash', 'heroku:gpt-oss-120b', 'heroku:kimi-k2-5', 'heroku:kimi-k2-thinking', 'heroku:minimax-m2', 'heroku:minimax-m2-1', 'heroku:qwen3-235b', 'heroku:qwen3-coder-480b', 'heroku:nova-2-lite', 'heroku:nova-lite', 'heroku:nova-pro', 'huggingface:deepseek-ai/DeepSeek-R1', 'huggingface:meta-llama/Llama-3.3-70B-Instruct', 'huggingface:meta-llama/Llama-4-Maverick-17B-128E-Instruct', 'huggingface:meta-llama/Llama-4-Scout-17B-16E-Instruct', 'huggingface:Qwen/Qwen2.5-72B-Instruct', 'huggingface:Qwen/Qwen3-235B-A22B', 'huggingface:Qwen/Qwen3-32B', 'huggingface:Qwen/QwQ-32B', 'mistral:codestral-latest', 'mistral:mistral-large-latest', 'mistral:mistral-moderation-latest', 'mistral:mistral-small-latest', 'moonshotai:kimi-k2-0711-preview', 'moonshotai:kimi-latest', 'moonshotai:kimi-thinking-preview', 'moonshotai:moonshot-v1-128k-vision-preview', 'moonshotai:moonshot-v1-128k', 'moonshotai:moonshot-v1-32k-vision-preview', 'moonshotai:moonshot-v1-32k', 'moonshotai:moonshot-v1-8k-vision-preview', 'moonshotai:moonshot-v1-8k', 'openai:computer-use-preview-2025-03-11', 'openai:computer-use-preview', 'openai:gpt-3.5-turbo-0125', 'openai:gpt-3.5-turbo-0301', 'openai:gpt-3.5-turbo-0613', 'openai:gpt-3.5-turbo-1106', 'openai:gpt-3.5-turbo-16k-0613', 'openai:gpt-3.5-turbo-16k', 'openai:gpt-3.5-turbo', 'openai:gpt-4-0314', 'openai:gpt-4-0613', 'openai:gpt-4-turbo-2024-04-09', 'openai:gpt-4-turbo', 'openai:gpt-4.1-2025-04-14', 'openai:gpt-4.1-mini-2025-04-14', 'openai:gpt-4.1-mini', 'openai:gpt-4.1-nano-2025-04-14', 'openai:gpt-4.1-nano', 'openai:gpt-4.1', 'openai:gpt-4', 'openai:gpt-4o-2024-05-13', 'openai:gpt-4o-2024-08-06', 'openai:gpt-4o-2024-11-20', 'openai:gpt-4o-audio-preview-2024-12-17', 'openai:gpt-4o-audio-preview-2025-06-03', 'openai:gpt-4o-audio-preview', 'openai:gpt-4o-mini-2024-07-18', 'openai:gpt-4o-mini-audio-preview-2024-12-17', 'openai:gpt-4o-mini-audio-preview', 'openai:gpt-4o-mini-search-preview-2025-03-11', 'openai:gpt-4o-mini-search-preview', 'openai:gpt-4o-mini', 'openai:gpt-4o-search-preview-2025-03-11', 'openai:gpt-4o-search-preview', 'openai:gpt-4o', 'openai:gpt-5-2025-08-07', 'openai:gpt-5-chat-latest', 'openai:gpt-5-codex', 'openai:gpt-5-mini-2025-08-07', 'openai:gpt-5-mini', 'openai:gpt-5-nano-2025-08-07', 'openai:gpt-5-nano', 'openai:gpt-5-pro-2025-10-06', 'openai:gpt-5-pro', 'openai:gpt-5.1-2025-11-13', 'openai:gpt-5.1-chat-latest', 'openai:gpt-5.1-codex-max', 'openai:gpt-5.1-codex', 'openai:gpt-5.1', 'openai:gpt-5.2-2025-12-11', 'openai:gpt-5.2-chat-latest', 'openai:gpt-5.2-pro-2025-12-11', 'openai:gpt-5.2-pro', 'openai:gpt-5.2', 'openai:gpt-5.3-chat-latest', 'openai:gpt-5.4-mini-2026-03-17', 'openai:gpt-5.4-mini', 'openai:gpt-5.4-nano-2026-03-17', 'openai:gpt-5.4-nano', 'openai:gpt-5.4', 'openai:gpt-5', 'openai:o1-2024-12-17', 'openai:o1-pro-2025-03-19', 'openai:o1-pro', 'openai:o1', 'openai:o3-2025-04-16', 'openai:o3-deep-research-2025-06-26', 'openai:o3-deep-research', 'openai:o3-mini-2025-01-31', 'openai:o3-mini', 'openai:o3-pro-2025-06-10', 'openai:o3-pro', 'openai:o3', 'openai:o4-mini-2025-04-16', 'openai:o4-mini-deep-research-2025-06-26', 'openai:o4-mini-deep-research', 'openai:o4-mini', 'openai-chat:computer-use-preview-2025-03-11', 'openai-chat:computer-use-preview', 'openai-chat:gpt-3.5-turbo-0125', 'openai-chat:gpt-3.5-turbo-0301', 'openai-chat:gpt-3.5-turbo-0613', 'openai-chat:gpt-3.5-turbo-1106', 'openai-chat:gpt-3.5-turbo-16k-0613', 'openai-chat:gpt-3.5-turbo-16k', 'openai-chat:gpt-3.5-turbo', 'openai-chat:gpt-4-0314', 'openai-chat:gpt-4-0613', 'openai-chat:gpt-4-turbo-2024-04-09', 'openai-chat:gpt-4-turbo', 'openai-chat:gpt-4.1-2025-04-14', 'openai-chat:gpt-4.1-mini-2025-04-14', 'openai-chat:gpt-4.1-mini', 'openai-chat:gpt-4.1-nano-2025-04-14', 'openai-chat:gpt-4.1-nano', 'openai-chat:gpt-4.1', 'openai-chat:gpt-4', 'openai-chat:gpt-4o-2024-05-13', 'openai-chat:gpt-4o-2024-08-06', 'openai-chat:gpt-4o-2024-11-20', 'openai-chat:gpt-4o-audio-preview-2024-12-17', 'openai-chat:gpt-4o-audio-preview-2025-06-03', 'openai-chat:gpt-4o-audio-preview', 'openai-chat:gpt-4o-mini-2024-07-18', 'openai-chat:gpt-4o-mini-audio-preview-2024-12-17', 'openai-chat:gpt-4o-mini-audio-preview', 'openai-chat:gpt-4o-mini-search-preview-2025-03-11', 'openai-chat:gpt-4o-mini-search-preview', 'openai-chat:gpt-4o-mini', 'openai-chat:gpt-4o-search-preview-2025-03-11', 'openai-chat:gpt-4o-search-preview', 'openai-chat:gpt-4o', 'openai-chat:gpt-5-2025-08-07', 'openai-chat:gpt-5-chat-latest', 'openai-chat:gpt-5-codex', 'openai-chat:gpt-5-mini-2025-08-07', 'openai-chat:gpt-5-mini', 'openai-chat:gpt-5-nano-2025-08-07', 'openai-chat:gpt-5-nano', 'openai-chat:gpt-5-pro-2025-10-06', 'openai-chat:gpt-5-pro', 'openai-chat:gpt-5.1-2025-11-13', 'openai-chat:gpt-5.1-chat-latest', 'openai-chat:gpt-5.1-codex-max', 'openai-chat:gpt-5.1-codex', 'openai-chat:gpt-5.1', 'openai-chat:gpt-5.2-2025-12-11', 'openai-chat:gpt-5.2-chat-latest', 'openai-chat:gpt-5.2-pro-2025-12-11', 'openai-chat:gpt-5.2-pro', 'openai-chat:gpt-5.2', 'openai-chat:gpt-5.3-chat-latest', 'openai-chat:gpt-5.4-mini-2026-03-17', 'openai-chat:gpt-5.4-mini', 'openai-chat:gpt-5.4-nano-2026-03-17', 'openai-chat:gpt-5.4-nano', 'openai-chat:gpt-5.4', 'openai-chat:gpt-5', 'openai-chat:o1-2024-12-17', 'openai-chat:o1-pro-2025-03-19', 'openai-chat:o1-pro', 'openai-chat:o1', 'openai-chat:o3-2025-04-16', 'openai-chat:o3-deep-research-2025-06-26', 'openai-chat:o3-deep-research', 'openai-chat:o3-mini-2025-01-31', 'openai-chat:o3-mini', 'openai-chat:o3-pro-2025-06-10', 'openai-chat:o3-pro', 'openai-chat:o3', 'openai-chat:o4-mini-2025-04-16', 'openai-chat:o4-mini-deep-research-2025-06-26', 'openai-chat:o4-mini-deep-research', 'openai-chat:o4-mini', 'test'])` ### ALLOW\_MODEL\_REQUESTS Whether to allow requests to models. This global setting allows you to disable request to most models, e.g. to make sure you don't accidentally make costly requests to a model during tests. The testing models [`TestModel`](/docs/ai/api/models/test/#pydantic_ai.models.test.TestModel) and [`FunctionModel`](/docs/ai/api/models/function/#pydantic_ai.models.function.FunctionModel) are no affected by this setting. **Default:** `True` --- # [pydantic_ai.models.bedrock](https://pydantic.dev/docs/ai/api/models/bedrock/) # pydantic\_ai.models.bedrock ## Setup For details on how to set up authentication with this model, see [model configuration for Bedrock](/docs/ai/models/bedrock). ### BedrockModelSettings **Bases:** [`ModelSettings`](/docs/ai/api/pydantic-ai/settings/#pydantic_ai.settings.ModelSettings) Settings for Bedrock models. See [the Bedrock Converse API docs](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_Converse.html#API_runtime_Converse_RequestSyntax) for a full list. See [the boto3 implementation](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/bedrock-runtime/client/converse.html) of the Bedrock Converse API. #### Attributes ##### bedrock\_guardrail\_config Content moderation and safety settings for Bedrock API requests. See more about it on [https://docs.aws.amazon.com/bedrock/latest/APIReference/API\_runtime\_GuardrailConfiguration.html](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_GuardrailConfiguration.html). **Type:** `GuardrailConfigurationTypeDef` ##### bedrock\_performance\_configuration Performance optimization settings for model inference. See more about it on [https://docs.aws.amazon.com/bedrock/latest/APIReference/API\_runtime\_PerformanceConfiguration.html](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_PerformanceConfiguration.html). **Type:** `PerformanceConfigurationTypeDef` ##### bedrock\_request\_metadata Additional metadata to attach to Bedrock API requests. See more about it on [https://docs.aws.amazon.com/bedrock/latest/APIReference/API\_runtime\_Converse.html#API\_runtime\_Converse\_RequestSyntax](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_Converse.html#API_runtime_Converse_RequestSyntax). **Type:** [`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), [`str`](https://docs.python.org/3/library/stdtypes.html#str)\] ##### bedrock\_additional\_model\_response\_fields\_paths JSON paths to extract additional fields from model responses. See more about it on [https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters.html](https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters.html). **Type:** [`list`](https://docs.python.org/3/glossary.html#term-list)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str)\] ##### bedrock\_prompt\_variables Variables for substitution into prompt templates. See more about it on [https://docs.aws.amazon.com/bedrock/latest/APIReference/API\_runtime\_PromptVariableValues.html](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_PromptVariableValues.html). **Type:** [`Mapping`](https://docs.python.org/3/library/typing.html#typing.Mapping)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), `PromptVariableValuesTypeDef`\] ##### bedrock\_additional\_model\_requests\_fields Additional model-specific parameters to include in requests. See more about it on [https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters.html](https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters.html). **Type:** [`Mapping`](https://docs.python.org/3/library/typing.html#typing.Mapping)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] ##### bedrock\_cache\_tool\_definitions Whether to add a cache point after the last tool definition. When enabled, the last tool in the `tools` array will include a `cachePoint`, allowing Bedrock to cache tool definitions and reduce costs for compatible models. Set to `True` or `'5m'` for a 5-minute TTL (the default), or `'1h'` for a 1-hour TTL. See [https://docs.aws.amazon.com/bedrock/latest/userguide/prompt-caching.html](https://docs.aws.amazon.com/bedrock/latest/userguide/prompt-caching.html) for more information. **Type:** [`bool`](https://docs.python.org/3/library/functions.html#bool) | [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['5m', '1h'\] ##### bedrock\_cache\_instructions Whether to add a cache point after the system prompt blocks. When enabled, an extra `cachePoint` is appended to the system prompt so Bedrock can cache system instructions. Set to `True` or `'5m'` for a 5-minute TTL (the default), or `'1h'` for a 1-hour TTL. See [https://docs.aws.amazon.com/bedrock/latest/userguide/prompt-caching.html](https://docs.aws.amazon.com/bedrock/latest/userguide/prompt-caching.html) for more information. **Type:** [`bool`](https://docs.python.org/3/library/functions.html#bool) | [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['5m', '1h'\] ##### bedrock\_cache\_messages Convenience setting to enable caching for the last user message. When enabled, this automatically adds a cache point to the last content block in the final user message, which is useful for caching conversation history or context in multi-turn conversations. Set to `True` or `'5m'` for a 5-minute TTL (the default), or `'1h'` for a 1-hour TTL. Note: Uses 1 of Bedrock's 4 available cache points per request. Any additional CachePoint markers in messages will be automatically limited to respect the 4-cache-point maximum. See [https://docs.aws.amazon.com/bedrock/latest/userguide/prompt-caching.html](https://docs.aws.amazon.com/bedrock/latest/userguide/prompt-caching.html) for more information. **Type:** [`bool`](https://docs.python.org/3/library/functions.html#bool) | [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['5m', '1h'\] ##### bedrock\_service\_tier Setting for optimizing performance and cost. Accepts `{'type': 'default' | 'flex' | 'priority' | 'reserved'}`. Takes precedence over the top-level [`service_tier`](/docs/ai/api/pydantic-ai/settings/#pydantic_ai.settings.ModelSettings.service_tier), and is the only way to request `'reserved'` (which requires a pre-purchased capacity reservation). See more about it on [https://docs.aws.amazon.com/bedrock/latest/userguide/service-tiers-inference.html](https://docs.aws.amazon.com/bedrock/latest/userguide/service-tiers-inference.html). **Type:** `ServiceTierTypeDef` ##### bedrock\_inference\_profile An [inference profile](https://docs.aws.amazon.com/bedrock/latest/userguide/inference-profiles.html) ARN to use as the `modelId` in API requests. When set, this value is used as the `modelId` in `converse` and `converse_stream` API calls instead of the base `model_name`. This allows you to pass the base model name (e.g. `'anthropic.claude-sonnet-4-5-20250929-v1:0'`) as `model_name` for detecting model capabilities and token counting, while routing requests through an inference profile for cost tracking or cross-region inference. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ### BedrockConverseModel **Bases:** `Model[BaseClient]` A model that uses the Bedrock Converse API. #### Attributes ##### client The boto3 client used to make requests to the Bedrock Converse API. Defaults to the client from the [`Provider`](/docs/ai/api/pydantic-ai/providers/#pydantic_ai.providers.Provider). It can be reassigned, e.g. to rotate short-lived credentials in a long-running service, but prefer assigning to `BedrockProvider.client` so all models sharing the provider pick up the new client. Once you've assigned a client here, you're responsible for keeping it valid; the provider's client is no longer consulted. **Type:** `BedrockRuntimeClient` ##### model\_name The model name. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### system The model provider. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) #### Methods ##### \_\_init\_\_ ```python def __init__( model_name: BedrockModelName, provider: Literal['bedrock', 'gateway'] | Provider[BaseClient] = 'bedrock', profile: ModelProfileSpec | None = None, settings: ModelSettings | None = None, ) ``` Initialize a Bedrock model. ###### Parameters **`model_name`** : `BedrockModelName` The name of the model to use. **`model_name`** : `BedrockModelName` The name of the Bedrock model to use. List of model names available [here](https://docs.aws.amazon.com/bedrock/latest/userguide/models-supported.html). **`provider`** : [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['bedrock', 'gateway'\] | `Provider`\[`BaseClient`\] _Default:_ `'bedrock'` The provider to use for authentication and API access. Can be either the string 'bedrock' or an instance of `Provider[BaseClient]`. If not provided, a new provider will be created using the other parameters. **`profile`** : `ModelProfileSpec` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` The model profile to use. Defaults to a profile picked by the provider based on the model name. **`settings`** : [`ModelSettings`](/docs/ai/api/pydantic-ai/settings/#pydantic_ai.settings.ModelSettings) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Model-specific settings that will be used as defaults for this model. ##### supported\_native\_tools `@classmethod` ```python def supported_native_tools(cls) -> frozenset[type[AbstractNativeTool]] ``` The set of builtin tool types this model can handle. ###### Returns [`frozenset`](https://docs.python.org/3/library/stdtypes.html#frozenset)\[[`type`](https://docs.python.org/3/glossary.html#term-type)\[`AbstractNativeTool`\]\] ##### count\_tokens `@async` ```python def count_tokens( messages: list[ModelMessage], model_settings: ModelSettings | None, model_request_parameters: ModelRequestParameters, ) -> usage.RequestUsage ``` Count the number of tokens, works with limited models. Check the actual supported models on [https://docs.aws.amazon.com/bedrock/latest/userguide/count-tokens.html](https://docs.aws.amazon.com/bedrock/latest/userguide/count-tokens.html) ###### Returns [`usage.RequestUsage`](/docs/ai/api/pydantic-ai/usage/#pydantic_ai.usage.RequestUsage) ### BedrockStreamedResponse **Bases:** `StreamedResponse` Implementation of `StreamedResponse` for Bedrock models. #### Attributes ##### model\_name Get the model name of the response. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### provider\_name Get the provider name. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### provider\_url Get the provider base URL. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ### LatestBedrockModelNames Latest Bedrock models. **Default:** `Literal['amazon.titan-tg1-large', 'amazon.titan-text-lite-v1', 'amazon.titan-text-express-v1', 'us.amazon.nova-2-lite-v1:0', 'us.amazon.nova-pro-v1:0', 'us.amazon.nova-lite-v1:0', 'us.amazon.nova-micro-v1:0', 'anthropic.claude-3-5-sonnet-20241022-v2:0', 'us.anthropic.claude-3-5-sonnet-20241022-v2:0', 'anthropic.claude-3-5-haiku-20241022-v1:0', 'us.anthropic.claude-3-5-haiku-20241022-v1:0', 'anthropic.claude-instant-v1', 'anthropic.claude-v2:1', 'anthropic.claude-v2', 'anthropic.claude-3-sonnet-20240229-v1:0', 'us.anthropic.claude-3-sonnet-20240229-v1:0', 'anthropic.claude-3-haiku-20240307-v1:0', 'us.anthropic.claude-3-haiku-20240307-v1:0', 'anthropic.claude-3-opus-20240229-v1:0', 'us.anthropic.claude-3-opus-20240229-v1:0', 'anthropic.claude-3-5-sonnet-20240620-v1:0', 'us.anthropic.claude-3-5-sonnet-20240620-v1:0', 'anthropic.claude-3-7-sonnet-20250219-v1:0', 'us.anthropic.claude-3-7-sonnet-20250219-v1:0', 'anthropic.claude-opus-4-20250514-v1:0', 'us.anthropic.claude-opus-4-20250514-v1:0', 'global.anthropic.claude-opus-4-5-20251101-v1:0', 'anthropic.claude-sonnet-4-20250514-v1:0', 'us.anthropic.claude-sonnet-4-20250514-v1:0', 'eu.anthropic.claude-sonnet-4-20250514-v1:0', 'anthropic.claude-sonnet-4-5-20250929-v1:0', 'us.anthropic.claude-sonnet-4-5-20250929-v1:0', 'eu.anthropic.claude-sonnet-4-5-20250929-v1:0', 'anthropic.claude-sonnet-4-6', 'us.anthropic.claude-sonnet-4-6', 'eu.anthropic.claude-sonnet-4-6', 'anthropic.claude-haiku-4-5-20251001-v1:0', 'us.anthropic.claude-haiku-4-5-20251001-v1:0', 'eu.anthropic.claude-haiku-4-5-20251001-v1:0', 'cohere.command-text-v14', 'cohere.command-r-v1:0', 'cohere.command-r-plus-v1:0', 'cohere.command-light-text-v14', 'meta.llama3-8b-instruct-v1:0', 'meta.llama3-70b-instruct-v1:0', 'meta.llama3-1-8b-instruct-v1:0', 'us.meta.llama3-1-8b-instruct-v1:0', 'meta.llama3-1-70b-instruct-v1:0', 'us.meta.llama3-1-70b-instruct-v1:0', 'meta.llama3-1-405b-instruct-v1:0', 'us.meta.llama3-2-11b-instruct-v1:0', 'us.meta.llama3-2-90b-instruct-v1:0', 'us.meta.llama3-2-1b-instruct-v1:0', 'us.meta.llama3-2-3b-instruct-v1:0', 'us.meta.llama3-3-70b-instruct-v1:0', 'mistral.mistral-7b-instruct-v0:2', 'mistral.mixtral-8x7b-instruct-v0:1', 'mistral.mistral-large-2402-v1:0', 'mistral.mistral-large-2407-v1:0']` ### BedrockModelName Possible Bedrock model names. Since Bedrock supports a variety of date-stamped models, we explicitly list the latest models but allow any name in the type hints. See [the Bedrock docs](https://docs.aws.amazon.com/bedrock/latest/userguide/models-supported.html) for a full list. **Default:** `str | LatestBedrockModelNames` --- # [pydantic_ai.models.cerebras](https://pydantic.dev/docs/ai/api/models/cerebras/) # pydantic\_ai.models.cerebras ## Setup For details on how to set up authentication with this model, see [model configuration for Cerebras](/docs/ai/models/cerebras). Cerebras model implementation using OpenAI-compatible API. ### CerebrasModelSettings **Bases:** [`ModelSettings`](/docs/ai/api/pydantic-ai/settings/#pydantic_ai.settings.ModelSettings) Settings used for a Cerebras model request. ALL FIELDS MUST BE `cerebras_` PREFIXED SO YOU CAN MERGE THEM WITH OTHER MODELS. #### Attributes ##### cerebras\_disable\_reasoning Disable reasoning for the model. This setting is only supported on reasoning models: `zai-glm-4.6` and `gpt-oss-120b`. See [the Cerebras docs](https://inference-docs.cerebras.ai/resources/openai#passing-non-standard-parameters) for more details. **Type:** [`bool`](https://docs.python.org/3/library/functions.html#bool) ### CerebrasModel **Bases:** `OpenAIChatModel` A model that uses Cerebras's OpenAI-compatible API. Cerebras provides ultra-fast inference powered by the Wafer-Scale Engine (WSE). Apart from `__init__`, all methods are private or match those of the base class. #### Methods ##### \_\_init\_\_ ```python def __init__( model_name: CerebrasModelName, provider: Literal['cerebras'] | Provider[AsyncOpenAI] = 'cerebras', profile: ModelProfileSpec | None = None, settings: CerebrasModelSettings | None = None, ) ``` Initialize a Cerebras model. ###### Parameters **`model_name`** : `CerebrasModelName` The name of the Cerebras model to use. **`provider`** : [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['cerebras'\] | `Provider`\[`AsyncOpenAI`\] _Default:_ `'cerebras'` The provider to use. Defaults to 'cerebras'. **`profile`** : `ModelProfileSpec` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` The model profile to use. Defaults to a profile based on the model name. **`settings`** : `CerebrasModelSettings` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Model-specific settings that will be used as defaults for this model. ### CerebrasModelName Possible Cerebras model names. Since Cerebras supports a variety of models and the list changes frequently, we explicitly list known models but allow any name in the type hints. See [https://inference-docs.cerebras.ai/models/overview](https://inference-docs.cerebras.ai/models/overview) for an up to date list of models. **Default:** `str | LatestCerebrasModelNames` --- # [pydantic_ai.models.cohere](https://pydantic.dev/docs/ai/api/models/cohere/) # pydantic\_ai.models.cohere ## Setup For details on how to set up authentication with this model, see [model configuration for Cohere](/docs/ai/models/cohere). ### CohereModelSettings **Bases:** [`ModelSettings`](/docs/ai/api/pydantic-ai/settings/#pydantic_ai.settings.ModelSettings) Settings used for a Cohere model request. ### CohereModel **Bases:** `Model[AsyncClientV2]` A model that uses the Cohere API. Internally, this uses the [Cohere Python client](https://github.com/cohere-ai/cohere-python) to interact with the API. Apart from `__init__`, all methods are private or match those of the base class. #### Attributes ##### model\_name The model name. **Type:** `CohereModelName` ##### system The model provider. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) #### Methods ##### \_\_init\_\_ ```python def __init__( model_name: CohereModelName, provider: Literal['cohere'] | Provider[AsyncClientV2] = 'cohere', profile: ModelProfileSpec | None = None, settings: ModelSettings | None = None, ) ``` Initialize an Cohere model. ###### Parameters **`model_name`** : `CohereModelName` The name of the Cohere model to use. List of model names available [here](https://docs.cohere.com/docs/models#command). **`provider`** : [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['cohere'\] | `Provider`\[`AsyncClientV2`\] _Default:_ `'cohere'` The provider to use for authentication and API access. Can be either the string 'cohere' or an instance of `Provider[AsyncClientV2]`. If not provided, a new provider will be created using the other parameters. **`profile`** : `ModelProfileSpec` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` The model profile to use. Defaults to a profile picked by the provider based on the model name. **`settings`** : [`ModelSettings`](/docs/ai/api/pydantic-ai/settings/#pydantic_ai.settings.ModelSettings) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Model-specific settings that will be used as defaults for this model. ### LatestCohereModelNames Latest Cohere models. **Default:** `Literal['c4ai-aya-expanse-32b', 'c4ai-aya-expanse-8b', 'command-nightly', 'command-r-08-2024', 'command-r-plus-08-2024', 'command-r7b-12-2024']` ### CohereModelName Possible Cohere model names. Since Cohere supports a variety of date-stamped models, we explicitly list the latest models but allow any name in the type hints. See [Cohere's docs](https://docs.cohere.com/v2/docs/models) for a list of all available models. **Default:** `str | LatestCohereModelNames` --- # [pydantic_ai.models.fallback](https://pydantic.dev/docs/ai/api/models/fallback/) # pydantic\_ai.models.fallback ### ResponseRejected **Bases:** [`Exception`](https://docs.python.org/3/library/exceptions.html#Exception) Raised within a `FallbackExceptionGroup` when model responses are rejected by a response handler. ### FallbackModel **Bases:** `Model` A model that uses one or more fallback models upon failure. Apart from `__init__`, all methods are private or match those of the base class. #### Attributes ##### model\_name The model name. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### model\_id The fully qualified model identifier, combining the wrapped models' IDs. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) #### Methods ##### \_\_init\_\_ ```python def __init__( default_model: Model | KnownModelName | str, fallback_models: Model | KnownModelName | str = (), fallback_on: FallbackOn = (ModelAPIError,), ) ``` Initialize a fallback model instance. ###### Parameters **`default_model`** : `Model` | `KnownModelName` | [`str`](https://docs.python.org/3/library/stdtypes.html#str) The name or instance of the default model to use. **`fallback_models`** : `Model` | `KnownModelName` | [`str`](https://docs.python.org/3/library/stdtypes.html#str) _Default:_ `()` The names or instances of the fallback models to use upon failure. **`fallback_on`** : `FallbackOn` _Default:_ `(ModelAPIError,)` Conditions that trigger fallback to the next model. Accepts: - A tuple of exception types: `(ModelAPIError, RateLimitError)` - An exception handler (sync or async): `lambda exc: isinstance(exc, MyError)` - A response handler (sync or async): `def check(r: ModelResponse) -> bool` - A sequence mixing all of the above: `[ModelAPIError, exc_handler, response_handler]` Handler type is auto-detected by inspecting type hints on the first parameter. If the first parameter is hinted as `ModelResponse`, it's a response handler. Otherwise (including untyped handlers and lambdas), it's an exception handler. ##### \_\_aenter\_\_ `@async` ```python def __aenter__() -> FallbackModel ``` Enter all sub-models so their providers can manage HTTP client lifecycle. ###### Returns `FallbackModel` ##### \_\_aexit\_\_ `@async` ```python def __aexit__( exc_type: type[BaseException] | None, exc_val: BaseException | None, exc_tb: TracebackType | None, ) -> bool | None ``` Exit all sub-models, closing their providers' HTTP clients. ###### Returns [`bool`](https://docs.python.org/3/library/functions.html#bool) | [`None`](https://docs.python.org/3/library/constants.html#None) ##### request `@async` ```python def request( messages: list[ModelMessage], model_settings: ModelSettings | None, model_request_parameters: ModelRequestParameters, ) -> ModelResponse ``` Try each model in sequence until one succeeds. In case of failure, raise a FallbackExceptionGroup with all exceptions. ###### Returns [`ModelResponse`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.ModelResponse) ##### request\_stream `@async` ```python def request_stream( messages: list[ModelMessage], model_settings: ModelSettings | None, model_request_parameters: ModelRequestParameters, run_context: RunContext[Any] | None = None, ) -> AsyncIterator[StreamedResponse] ``` Try each model in sequence until one succeeds. ###### Returns [`AsyncIterator`](https://docs.python.org/3/library/typing.html#typing.AsyncIterator)\[`StreamedResponse`\] ### ExceptionHandler A sync or async callable that decides whether an exception should trigger fallback. **Default:** `Callable[[Exception], Awaitable[bool]] | Callable[[Exception], bool]` ### ResponseHandler A sync or async callable that decides whether a model response should trigger fallback. **Default:** `Callable[[ModelResponse], Awaitable[bool]] | Callable[[ModelResponse], bool]` ### FallbackOn The type of the `fallback_on` parameter to [`FallbackModel`](/docs/ai/api/models/fallback/#pydantic_ai.models.fallback.FallbackModel). **Default:** `type[Exception] | tuple[type[Exception], ...] | ExceptionHandler | ResponseHandler | Sequence[type[Exception] | ExceptionHandler | ResponseHandler]` --- # [pydantic_ai.models.function](https://pydantic.dev/docs/ai/api/models/function/) # pydantic\_ai.models.function A model controlled by a local function. [`FunctionModel`](/docs/ai/api/models/function/#pydantic_ai.models.function.FunctionModel) is similar to [`TestModel`](/docs/ai/models/test), but allows greater control over the model's behavior. Its primary use case is for more advanced unit testing than is possible with `TestModel`. Here's a minimal example: function\_model\_usage.py ```python from pydantic_ai import Agent from pydantic_ai import ModelMessage, ModelResponse, TextPart from pydantic_ai.models.function import FunctionModel, AgentInfo my_agent = Agent('openai:gpt-5.2') async def model_function( messages: list[ModelMessage], info: AgentInfo ) -> ModelResponse: print(messages) """ [ ModelRequest( parts=[ UserPromptPart( content='Testing my agent...', timestamp=datetime.datetime(...), ) ], timestamp=datetime.datetime(...), run_id='...', conversation_id='...', ) ] """ print(info) """ AgentInfo( function_tools=[], allow_text_output=True, output_tools=[], model_settings=None, model_request_parameters=ModelRequestParameters( function_tools=[], native_tools=[], output_tools=[] ), instructions=None, ) """ return ModelResponse(parts=[TextPart('hello world')]) async def test_my_agent(): """Unit test for my_agent, to be run by pytest.""" with my_agent.override(model=FunctionModel(model_function)): result = await my_agent.run('Testing my agent...') assert result.output == 'hello world' ``` See [Unit testing with `FunctionModel`](/docs/ai/guides/testing#unit-testing-with-functionmodel) for detailed documentation. ### FunctionModel **Bases:** `Model` A model controlled by a local function. Apart from `__init__`, all methods are private or match those of the base class. #### Attributes ##### model\_name The model name. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### system The system / model provider. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) #### Methods ##### \_\_init\_\_ ```python def __init__( function: FunctionDef, model_name: str | None = None, profile: ModelProfileSpec | None = None, settings: ModelSettings | None = None, ) -> None def __init__( stream_function: StreamFunctionDef, model_name: str | None = None, profile: ModelProfileSpec | None = None, settings: ModelSettings | None = None, ) -> None def __init__( function: FunctionDef, stream_function: StreamFunctionDef, model_name: str | None = None, profile: ModelProfileSpec | None = None, settings: ModelSettings | None = None, ) -> None ``` Initialize a `FunctionModel`. Either `function` or `stream_function` must be provided, providing both is allowed. ###### Parameters **`function`** : `FunctionDef` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` The function to call for non-streamed requests. **`stream_function`** : `StreamFunctionDef` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` The function to call for streamed requests. **`model_name`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` The name of the model. If not provided, a name is generated from the function names. **`profile`** : `ModelProfileSpec` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` The model profile to use. **`settings`** : [`ModelSettings`](/docs/ai/api/pydantic-ai/settings/#pydantic_ai.settings.ModelSettings) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Model-specific settings that will be used as defaults for this model. ##### supported\_native\_tools `@classmethod` ```python def supported_native_tools(cls) -> frozenset[type[AbstractNativeTool]] ``` FunctionModel supports all builtin tools for testing flexibility. ###### Returns [`frozenset`](https://docs.python.org/3/library/stdtypes.html#frozenset)\[[`type`](https://docs.python.org/3/glossary.html#term-type)\[`AbstractNativeTool`\]\] ### AgentInfo Information about an agent. This is passed as the second to functions used within [`FunctionModel`](/docs/ai/api/models/function/#pydantic_ai.models.function.FunctionModel). #### Attributes ##### function\_tools The function tools available on this agent. These are the tools registered via the [`tool`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.Agent.tool) and [`tool_plain`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.Agent.tool_plain) decorators. **Type:** [`list`](https://docs.python.org/3/glossary.html#term-list)\[[`ToolDefinition`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.ToolDefinition)\] ##### allow\_text\_output Whether a plain text output is allowed. **Type:** [`bool`](https://docs.python.org/3/library/functions.html#bool) ##### output\_tools The tools that can called to produce the final output of the run. **Type:** [`list`](https://docs.python.org/3/glossary.html#term-list)\[[`ToolDefinition`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.ToolDefinition)\] ##### model\_settings The model settings passed to the run call. **Type:** [`ModelSettings`](/docs/ai/api/pydantic-ai/settings/#pydantic_ai.settings.ModelSettings) | [`None`](https://docs.python.org/3/library/constants.html#None) ##### model\_request\_parameters The model request parameters passed to the run call. **Type:** `ModelRequestParameters` ##### instructions The instructions passed to model. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) ### DeltaToolCall Incremental change to a tool call. Used to describe a chunk when streaming structured responses. #### Attributes ##### name Incremental change to the name of the tool. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `None` ##### json\_args Incremental change to the arguments as JSON **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `None` ##### tool\_call\_id Incremental change to the tool call ID. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `None` ### DeltaThinkingPart Incremental change to a thinking part. Used to describe a chunk when streaming thinking responses. #### Attributes ##### content Incremental change to the thinking content. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `None` ##### signature Incremental change to the thinking signature. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `None` ### FunctionStreamedResponse **Bases:** `StreamedResponse` Implementation of `StreamedResponse` for [FunctionModel](/docs/ai/api/models/function/#pydantic_ai.models.function.FunctionModel). #### Attributes ##### model\_name Get the model name of the response. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### provider\_name Get the provider name. **Type:** [`None`](https://docs.python.org/3/library/constants.html#None) ##### provider\_url Get the provider base URL. **Type:** [`None`](https://docs.python.org/3/library/constants.html#None) ##### timestamp Get the timestamp of the response. **Type:** [`datetime`](https://docs.python.org/3/library/datetime.html#module-datetime) ### DeltaToolCalls A mapping of tool call IDs to incremental changes. **Type:** [`TypeAlias`](https://docs.python.org/3/library/typing.html#typing.TypeAlias) **Default:** `dict[int, DeltaToolCall]` ### DeltaThinkingCalls A mapping of thinking call IDs to incremental changes. **Type:** [`TypeAlias`](https://docs.python.org/3/library/typing.html#typing.TypeAlias) **Default:** `dict[int, DeltaThinkingPart]` ### FunctionDef A function used to generate a non-streamed response. **Type:** [`TypeAlias`](https://docs.python.org/3/library/typing.html#typing.TypeAlias) **Default:** `Callable[[list[ModelMessage], AgentInfo], ModelResponse | Awaitable[ModelResponse]]` ### StreamFunctionDef A function used to generate a streamed response. While this is defined as having return type of `AsyncIterator[str | DeltaToolCalls | DeltaThinkingCalls | BuiltinTools]`, it should really be considered as `AsyncIterator[str] | AsyncIterator[DeltaToolCalls] | AsyncIterator[DeltaThinkingCalls]`, E.g. you need to yield all text, all `DeltaToolCalls`, all `DeltaThinkingCalls`, or all `BuiltinToolCallsReturns`, not mix them. **Type:** [`TypeAlias`](https://docs.python.org/3/library/typing.html#typing.TypeAlias) **Default:** `Callable[[list[ModelMessage], AgentInfo], AsyncIterator[str | DeltaToolCalls | DeltaThinkingCalls | BuiltinToolCallsReturns]]` --- # [pydantic_ai.models.google](https://pydantic.dev/docs/ai/api/models/google/) # pydantic\_ai.models.google Interface that uses the [`google-genai`](https://pypi.org/project/google-genai/) package under the hood to access Google's Gemini models via both the Gemini API and Google Cloud (formerly known as Vertex AI). ## Setup For details on how to set up authentication with this model, see [model configuration for Google](/docs/ai/models/google). ### GoogleModelSettings **Bases:** [`ModelSettings`](/docs/ai/api/pydantic-ai/settings/#pydantic_ai.settings.ModelSettings) Settings used for a Gemini model request. #### Attributes ##### google\_safety\_settings The safety settings to use for the model. See [https://ai.google.dev/gemini-api/docs/safety-settings](https://ai.google.dev/gemini-api/docs/safety-settings) for more information. **Type:** [`list`](https://docs.python.org/3/glossary.html#term-list)\[`SafetySettingDict`\] ##### google\_thinking\_config The thinking configuration to use for the model. See [https://ai.google.dev/gemini-api/docs/thinking](https://ai.google.dev/gemini-api/docs/thinking) for more information. **Type:** `ThinkingConfigDict` ##### google\_labels User-defined metadata to break down billed charges. Only supported by the Vertex AI API. See the [Gemini API docs](https://cloud.google.com/vertex-ai/generative-ai/docs/multimodal/add-labels-to-api-calls) for use cases and limitations. **Type:** [`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), [`str`](https://docs.python.org/3/library/stdtypes.html#str)\] ##### google\_video\_resolution The video resolution to use for the model. See [https://ai.google.dev/api/generate-content#MediaResolution](https://ai.google.dev/api/generate-content#MediaResolution) for more information. **Type:** `MediaResolution` ##### google\_cached\_content The name of the cached content to use for the model. See [https://ai.google.dev/gemini-api/docs/caching](https://ai.google.dev/gemini-api/docs/caching) for more information. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### google\_logprobs Include log probabilities in the response. See [https://docs.cloud.google.com/vertex-ai/generative-ai/docs/multimodal/content-generation-parameters#log-probabilities-output-tokens](https://docs.cloud.google.com/vertex-ai/generative-ai/docs/multimodal/content-generation-parameters#log-probabilities-output-tokens) for more information. Note: Only supported for Vertex AI and non-streaming requests. These will be included in `ModelResponse.provider_details['logprobs']`. **Type:** [`bool`](https://docs.python.org/3/library/functions.html#bool) ##### google\_top\_logprobs Include log probabilities of the top n tokens in the response. See [https://docs.cloud.google.com/vertex-ai/generative-ai/docs/multimodal/content-generation-parameters#log-probabilities-output-tokens](https://docs.cloud.google.com/vertex-ai/generative-ai/docs/multimodal/content-generation-parameters#log-probabilities-output-tokens) for more information. Note: Only supported for Vertex AI and non-streaming requests. These will be included in `ModelResponse.provider_details['logprobs']`. **Type:** [`int`](https://docs.python.org/3/library/functions.html#int) ##### google\_cloud\_service\_tier The service tier to use for the model request when using Google Cloud. Controls routing for Provisioned Throughput, Flex PayGo, and Priority PayGo (e.g., `'pt_only'`, `'flex_only'`, `'priority_only'`). See [`GoogleCloudServiceTier`](/docs/ai/api/models/google/#pydantic_ai.models.google.GoogleCloudServiceTier) for all values, headers sent, and links to Google docs. **Type:** `GoogleCloudServiceTier` ##### google\_vertex\_service\_tier Deprecated: use `google_cloud_service_tier`. Will be removed in v2. **Type:** `GoogleCloudServiceTier` ##### google\_service\_tier Deprecated: use `service_tier` for Gemini API (GLA) or `google_cloud_service_tier` for Google Cloud. **Type:** `GoogleServiceTier` ### GoogleModel **Bases:** `Model[Client]` A model that uses Gemini via `generativelanguage.googleapis.com` API. This is implemented from scratch rather than using a dedicated SDK, good API documentation is available [here](https://ai.google.dev/api). Apart from `__init__`, all methods are private or match those of the base class. #### Attributes ##### model\_name The model name. **Type:** `GoogleModelName` ##### system The model provider. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) #### Methods ##### \_\_init\_\_ ```python def __init__( model_name: GoogleModelName, provider: Literal['google', 'google-cloud', 'gateway'] | Provider[Client] = 'google', profile: ModelProfileSpec | None = None, settings: ModelSettings | None = None, ) ``` Initialize a Gemini model. ###### Parameters **`model_name`** : `GoogleModelName` The name of the model to use. **`provider`** : [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['google', 'google-cloud', 'gateway'\] | `Provider`\[`Client`\] _Default:_ `'google'` The provider to use for authentication and API access. Can be either the string 'google' (Gemini API) or 'google-cloud' (Google Cloud, formerly known as Vertex AI), or an instance of `Provider[google.genai.AsyncClient]`. Defaults to 'google'. **`profile`** : `ModelProfileSpec` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` The model profile to use. Defaults to a profile picked by the provider based on the model name. **`settings`** : [`ModelSettings`](/docs/ai/api/pydantic-ai/settings/#pydantic_ai.settings.ModelSettings) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` The model settings to use. Defaults to None. ##### supported\_native\_tools `@classmethod` ```python def supported_native_tools(cls) -> frozenset[type[AbstractNativeTool]] ``` Return the set of native tool types this model can handle. ###### Returns [`frozenset`](https://docs.python.org/3/library/stdtypes.html#frozenset)\[[`type`](https://docs.python.org/3/glossary.html#term-type)\[`AbstractNativeTool`\]\] ### GeminiStreamedResponse **Bases:** `StreamedResponse` Implementation of `StreamedResponse` for the Gemini model. #### Attributes ##### model\_name Get the model name of the response. **Type:** `GoogleModelName` ##### provider\_name Get the provider name. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### provider\_url Get the provider base URL. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### timestamp Get the timestamp of the response. **Type:** [`datetime`](https://docs.python.org/3/library/datetime.html#module-datetime) ### LatestGoogleModelNames Latest Gemini models. **Default:** `Literal['gemini-flash-latest', 'gemini-flash-lite-latest', 'gemini-2.0-flash', 'gemini-2.0-flash-lite', 'gemini-2.5-flash', 'gemini-2.5-flash-preview-09-2025', 'gemini-2.5-flash-image', 'gemini-2.5-flash-lite', 'gemini-2.5-flash-lite-preview-09-2025', 'gemini-2.5-pro', 'gemini-3-flash-preview', 'gemini-3-pro-image-preview', 'gemini-3-pro-preview', 'gemini-3.1-flash-image-preview', 'gemini-3.1-flash-lite-preview', 'gemini-3.1-pro-preview']` ### GoogleModelName Possible Gemini model names. Since Gemini supports a variety of date-stamped models, we explicitly list the latest models but allow any name in the type hints. See [the Gemini API docs](https://ai.google.dev/gemini-api/docs/models/gemini#model-variations) for a full list. **Default:** `str | LatestGoogleModelNames` ### GoogleCloudServiceTier Values for the `google_cloud_service_tier` field on [`GoogleModelSettings`](/docs/ai/api/models/google/#pydantic_ai.models.google.GoogleModelSettings). Controls Google Cloud HTTP headers for [Provisioned Throughput](https://cloud.google.com/vertex-ai/generative-ai/docs/provisioned-throughput/use-provisioned-throughput) (PT), [Flex PayGo](https://cloud.google.com/vertex-ai/generative-ai/docs/flex-paygo), and [Priority PayGo](https://cloud.google.com/vertex-ai/generative-ai/docs/priority-paygo). - `'pt_then_on_demand'` (**default**): PT when quota allows, then standard on-demand spillover. No headers sent. - `'pt_only'`: PT only (`X-Vertex-AI-LLM-Request-Type: dedicated`). No on-demand spillover; returns 429 when over quota. - `'pt_then_flex'`: PT when quota allows, then [Flex PayGo](https://cloud.google.com/vertex-ai/generative-ai/docs/flex-paygo) spillover (`X-Vertex-AI-LLM-Shared-Request-Type: flex`). - `'pt_then_priority'`: PT when quota allows, then [Priority PayGo](https://cloud.google.com/vertex-ai/generative-ai/docs/priority-paygo) spillover (`X-Vertex-AI-LLM-Shared-Request-Type: priority`). - `'on_demand'`: Standard on-demand only (`X-Vertex-AI-LLM-Request-Type: shared`). Bypasses PT for this request. - `'flex_only'`: [Flex PayGo](https://cloud.google.com/vertex-ai/generative-ai/docs/flex-paygo) only (`X-Vertex-AI-LLM-Request-Type: shared` and `X-Vertex-AI-LLM-Shared-Request-Type: flex`). Bypasses PT. - `'priority_only'`: [Priority PayGo](https://cloud.google.com/vertex-ai/generative-ai/docs/priority-paygo) only (`X-Vertex-AI-LLM-Request-Type: shared` and `X-Vertex-AI-LLM-Shared-Request-Type: priority`). Bypasses PT. Not every model or region supports every value; see the linked Google docs. **Default:** `Literal['pt_then_on_demand', 'pt_only', 'pt_then_flex', 'pt_then_priority', 'on_demand', 'flex_only', 'priority_only']` ### GoogleVertexServiceTier Deprecated alias for [`GoogleCloudServiceTier`](/docs/ai/api/models/google/#pydantic_ai.models.google.GoogleCloudServiceTier). Use `GoogleCloudServiceTier` instead -- Google Cloud is the new name for the platform formerly known as Vertex AI. **Default:** `GoogleCloudServiceTier` ### GoogleServiceTier Deprecated alias for service tier values. Use [`service_tier`](/docs/ai/api/pydantic-ai/settings/#pydantic_ai.settings.ModelSettings.service_tier) for Gemini API (GLA) or [`google_cloud_service_tier`](/docs/ai/api/models/google/#pydantic_ai.models.google.GoogleModelSettings.google_cloud_service_tier) for Google Cloud. **Default:** `Literal['pt_then_on_demand', 'pt_only', 'pt_then_flex', 'pt_then_priority', 'on_demand', 'flex_only', 'priority_only']` --- # [pydantic_ai.models.groq](https://pydantic.dev/docs/ai/api/models/groq/) # pydantic\_ai.models.groq ## Setup For details on how to set up authentication with this model, see [model configuration for Groq](/docs/ai/models/groq). ### GroqModelSettings **Bases:** [`ModelSettings`](/docs/ai/api/pydantic-ai/settings/#pydantic_ai.settings.ModelSettings) Settings used for a Groq model request. #### Attributes ##### groq\_reasoning\_format The format of the reasoning output. See [the Groq docs](https://console.groq.com/docs/reasoning#reasoning-format) for more details. **Type:** [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['hidden', 'raw', 'parsed'\] ### GroqModel **Bases:** `Model[AsyncGroq]` A model that uses the Groq API. Internally, this uses the [Groq Python client](https://github.com/groq/groq-python) to interact with the API. Apart from `__init__`, all methods are private or match those of the base class. #### Attributes ##### model\_name The model name. **Type:** `GroqModelName` ##### system The model provider. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) #### Methods ##### \_\_init\_\_ ```python def __init__( model_name: GroqModelName, provider: Literal['groq', 'gateway'] | Provider[AsyncGroq] = 'groq', profile: ModelProfileSpec | None = None, settings: ModelSettings | None = None, ) ``` Initialize a Groq model. ###### Parameters **`model_name`** : `GroqModelName` The name of the Groq model to use. List of model names available [here](https://console.groq.com/docs/models). **`provider`** : [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['groq', 'gateway'\] | `Provider`\[`AsyncGroq`\] _Default:_ `'groq'` The provider to use for authentication and API access. Can be either the string 'groq' or an instance of `Provider[AsyncGroq]`. If not provided, a new provider will be created using the other parameters. **`profile`** : `ModelProfileSpec` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` The model profile to use. Defaults to a profile picked by the provider based on the model name. **`settings`** : [`ModelSettings`](/docs/ai/api/pydantic-ai/settings/#pydantic_ai.settings.ModelSettings) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Model-specific settings that will be used as defaults for this model. ##### supported\_native\_tools `@classmethod` ```python def supported_native_tools(cls) -> frozenset[type[AbstractNativeTool]] ``` Return the set of builtin tool types this model can handle. ###### Returns [`frozenset`](https://docs.python.org/3/library/stdtypes.html#frozenset)\[[`type`](https://docs.python.org/3/glossary.html#term-type)\[`AbstractNativeTool`\]\] ### GroqStreamedResponse **Bases:** `StreamedResponse` Implementation of `StreamedResponse` for Groq models. #### Attributes ##### model\_name Get the model name of the response. **Type:** `GroqModelName` ##### provider\_name Get the provider name. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### provider\_url Get the provider base URL. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### timestamp Get the timestamp of the response. **Type:** [`datetime`](https://docs.python.org/3/library/datetime.html#module-datetime) ### ProductionGroqModelNames Production Groq models from [https://console.groq.com/docs/models#production-models](https://console.groq.com/docs/models#production-models). **Default:** `Literal['llama-3.1-8b-instant', 'llama-3.3-70b-versatile', 'meta-llama/llama-guard-4-12b', 'openai/gpt-oss-120b', 'openai/gpt-oss-20b', 'whisper-large-v3', 'whisper-large-v3-turbo']` ### PreviewGroqModelNames Preview Groq models from [https://console.groq.com/docs/models#preview-models](https://console.groq.com/docs/models#preview-models). **Default:** `Literal['meta-llama/llama-4-maverick-17b-128e-instruct', 'meta-llama/llama-4-scout-17b-16e-instruct', 'meta-llama/llama-prompt-guard-2-22m', 'meta-llama/llama-prompt-guard-2-86m', 'moonshotai/kimi-k2-instruct-0905', 'openai/gpt-oss-safeguard-20b', 'playai-tts', 'playai-tts-arabic', 'qwen/qwen-3-32b']` ### GroqModelName Possible Groq model names. Since Groq supports a variety of models and the list changes frequently, we explicitly list the named models as of 2025-03-31 but allow any name in the type hints. See [https://console.groq.com/docs/models](https://console.groq.com/docs/models) for an up to date list of models and more details. **Default:** `str | ProductionGroqModelNames | PreviewGroqModelNames` --- # [pydantic_ai.models.huggingface](https://pydantic.dev/docs/ai/api/models/huggingface/) # pydantic\_ai.models.huggingface ## Setup For details on how to set up authentication with this model, see [model configuration for Hugging Face](/docs/ai/models/huggingface). ### HuggingFaceModelSettings **Bases:** [`ModelSettings`](/docs/ai/api/pydantic-ai/settings/#pydantic_ai.settings.ModelSettings) Settings used for a Hugging Face model request. ### HuggingFaceModel **Bases:** `Model[AsyncInferenceClient]` A model that uses Hugging Face Inference Providers. Internally, this uses the [HF Python client](https://github.com/huggingface/huggingface_hub) to interact with the API. Apart from `__init__`, all methods are private or match those of the base class. #### Attributes ##### base\_url The base URL of the provider. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### model\_name The model name. **Type:** `HuggingFaceModelName` ##### system The system / model provider. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) #### Methods ##### \_\_init\_\_ ```python def __init__( model_name: str, provider: Literal['huggingface'] | Provider[AsyncInferenceClient] = 'huggingface', profile: ModelProfileSpec | None = None, settings: ModelSettings | None = None, ) ``` Initialize a Hugging Face model. ###### Parameters **`model_name`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) The name of the Model to use. You can browse available models [here](https://huggingface.co/models?pipeline_tag=text-generation&inference_provider=all&sort=trending). **`provider`** : [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['huggingface'\] | `Provider`\[`AsyncInferenceClient`\] _Default:_ `'huggingface'` The provider to use for Hugging Face Inference Providers. Can be either the string 'huggingface' or an instance of `Provider[AsyncInferenceClient]`. If not provided, the other parameters will be used. **`profile`** : `ModelProfileSpec` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` The model profile to use. Defaults to a profile picked by the provider based on the model name. **`settings`** : [`ModelSettings`](/docs/ai/api/pydantic-ai/settings/#pydantic_ai.settings.ModelSettings) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Model-specific settings that will be used as defaults for this model. ### HuggingFaceStreamedResponse **Bases:** `StreamedResponse` Implementation of `StreamedResponse` for Hugging Face models. #### Attributes ##### model\_name Get the model name of the response. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### provider\_name Get the provider name. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### provider\_url Get the provider base URL. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### timestamp Get the timestamp of the response. **Type:** [`datetime`](https://docs.python.org/3/library/datetime.html#module-datetime) ### LatestHuggingFaceModelNames Latest Hugging Face models. **Default:** `Literal['deepseek-ai/DeepSeek-R1', 'meta-llama/Llama-3.3-70B-Instruct', 'meta-llama/Llama-4-Maverick-17B-128E-Instruct', 'meta-llama/Llama-4-Scout-17B-16E-Instruct', 'Qwen/QwQ-32B', 'Qwen/Qwen2.5-72B-Instruct', 'Qwen/Qwen3-235B-A22B', 'Qwen/Qwen3-32B']` ### HuggingFaceModelName Possible Hugging Face model names. You can browse available models [here](https://huggingface.co/models?pipeline_tag=text-generation&inference_provider=all&sort=trending). **Default:** `str | LatestHuggingFaceModelNames` --- # [pydantic_ai.models.instrumented](https://pydantic.dev/docs/ai/api/models/instrumented/) # pydantic\_ai.models.instrumented ### InstrumentationSettings Options for instrumenting models and agents with OpenTelemetry. Used in: - `Agent(instrument=...)` - [`Agent.instrument_all()`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.Agent.instrument_all) - [`InstrumentedModel`](/docs/ai/api/models/instrumented/#pydantic_ai.models.instrumented.InstrumentedModel) See the [Debugging and Monitoring guide](https://ai.pydantic.dev/logfire/) for more info. #### Methods ##### \_\_init\_\_ ```python def __init__( tracer_provider: TracerProvider | None = None, meter_provider: MeterProvider | None = None, include_binary_content: bool = True, include_content: bool = True, version: Literal[1, 2, 3, 4, 5] = DEFAULT_INSTRUMENTATION_VERSION, event_mode: Literal['attributes', 'logs'] = 'attributes', logger_provider: LoggerProvider | None = None, use_aggregated_usage_attribute_names: bool = False, ) ``` Create instrumentation options. ###### Parameters **`tracer_provider`** : `TracerProvider` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` The OpenTelemetry tracer provider to use. If not provided, the global tracer provider is used. Calling `logfire.configure()` sets the global tracer provider, so most users don't need this. **`meter_provider`** : `MeterProvider` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` The OpenTelemetry meter provider to use. If not provided, the global meter provider is used. Calling `logfire.configure()` sets the global meter provider, so most users don't need this. **`include_binary_content`** : [`bool`](https://docs.python.org/3/library/functions.html#bool) _Default:_ `True` Whether to include binary content in the instrumentation events. **`include_content`** : [`bool`](https://docs.python.org/3/library/functions.html#bool) _Default:_ `True` Whether to include prompts, completions, and tool call arguments and responses in the instrumentation events. **`version`** : [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\[1, 2, 3, 4, 5\] _Default:_ `DEFAULT_INSTRUMENTATION_VERSION` Version of the data format. This is unrelated to the Pydantic AI package version. Version 1 is based on the legacy event-based OpenTelemetry GenAI spec and will be removed in a future release. The parameters `event_mode` and `logger_provider` are only relevant for version 1. Version 2 uses the newer OpenTelemetry GenAI spec and stores messages in the following attributes: - `gen_ai.system_instructions` for instructions passed to the agent. - `gen_ai.input.messages` and `gen_ai.output.messages` on model request spans. - `pydantic_ai.all_messages` on agent run spans. Version 3 is the same as version 2, with additional support for thinking tokens. Version 4 is the same as version 3, with GenAI semantic conventions for multimodal content: URL-based media uses type='uri' with uri and mime\_type fields (and modality for image/audio/video). Inline binary content uses type='blob' with mime\_type and content fields (and modality for image/audio/video). [https://opentelemetry.io/docs/specs/semconv/gen-ai/non-normative/examples-llm-calls/#multimodal-inputs-example](https://opentelemetry.io/docs/specs/semconv/gen-ai/non-normative/examples-llm-calls/#multimodal-inputs-example) Version 5 is the same as version 4, but CallDeferred and ApprovalRequired exceptions no longer record an exception event or set the span status to ERROR -- the span is left as UNSET, since deferrals are control flow, not errors. **`event_mode`** : [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['attributes', 'logs'\] _Default:_ `'attributes'` The mode for emitting events in version 1. If `'attributes'`, events are attached to the span as attributes. If `'logs'`, events are emitted as OpenTelemetry log-based events. **`logger_provider`** : `LoggerProvider` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` The OpenTelemetry logger provider to use. If not provided, the global logger provider is used. Calling `logfire.configure()` sets the global logger provider, so most users don't need this. This is only used if `event_mode='logs'` and `version=1`. **`use_aggregated_usage_attribute_names`** : [`bool`](https://docs.python.org/3/library/functions.html#bool) _Default:_ `False` Whether to use `gen_ai.aggregated_usage.*` attribute names for token usage on agent run spans instead of the standard `gen_ai.usage.*` names. Enable this to prevent double-counting in observability backends that aggregate span attributes across parent and child spans. Defaults to False. Note: `gen_ai.aggregated_usage.*` is a custom namespace, not part of the OpenTelemetry Semantic Conventions. It may be updated if OTel introduces an official convention. ##### messages\_to\_otel\_events ```python def messages_to_otel_events( messages: list[ModelMessage], parameters: ModelRequestParameters | None = None, ) -> list[LogRecord] ``` Convert a list of model messages to OpenTelemetry events. ###### Returns [`list`](https://docs.python.org/3/glossary.html#term-list)\[`LogRecord`\] -- A list of OpenTelemetry events. ###### Parameters **`messages`** : [`list`](https://docs.python.org/3/glossary.html#term-list)\[[`ModelMessage`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.ModelMessage)\] The messages to convert. **`parameters`** : `ModelRequestParameters` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` The model request parameters. ### InstrumentedModel **Bases:** `WrapperModel` Model which wraps another model so that requests are instrumented with OpenTelemetry. See the [Debugging and Monitoring guide](https://ai.pydantic.dev/logfire/) for more info. #### Attributes ##### instrumentation\_settings Instrumentation settings for this model. **Type:** [`InstrumentationSettings`](/docs/ai/api/models/instrumented/#pydantic_ai.models.instrumented.InstrumentationSettings) **Default:** `options or InstrumentationSettings()` ### instrument\_model ```python def instrument_model(model: Model, instrument: InstrumentationSettings | bool) -> Model ``` Wrap `model` in an `InstrumentedModel` so OTel/Logfire spans are emitted around requests. #### Returns `Model` --- # [pydantic_ai.models.mcp_sampling](https://pydantic.dev/docs/ai/api/models/mcp-sampling/) # pydantic\_ai.models.mcp\_sampling ### MCPSamplingModelSettings **Bases:** [`ModelSettings`](/docs/ai/api/pydantic-ai/settings/#pydantic_ai.settings.ModelSettings) Settings used for an MCP Sampling model request. #### Attributes ##### mcp\_model\_preferences Model preferences to use for MCP Sampling. **Type:** `ModelPreferences` ### MCPSamplingModel **Bases:** `Model` A model that uses MCP Sampling. [MCP Sampling](https://modelcontextprotocol.io/docs/concepts/sampling) allows an MCP server to make requests to a model by calling back to the MCP client that connected to it. #### Attributes ##### session The MCP server session to use for sampling. **Type:** `ServerSession` ##### default\_max\_tokens Default max tokens to use if not set in [`ModelSettings`](/docs/ai/api/pydantic-ai/settings/#pydantic_ai.settings.ModelSettings.max_tokens). Max tokens is a required parameter for MCP Sampling, but optional on [`ModelSettings`](/docs/ai/api/pydantic-ai/settings/#pydantic_ai.settings.ModelSettings), so this value is used as fallback. **Type:** [`int`](https://docs.python.org/3/library/functions.html#int) **Default:** `16384` ##### model\_name The model name. Since the model name isn't known until the request is made, this property always returns `'mcp-sampling'`. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### system The system / model provider, returns `'MCP'`. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) --- # [pydantic_ai.models.mistral](https://pydantic.dev/docs/ai/api/models/mistral/) # pydantic\_ai.models.mistral ## Setup For details on how to set up authentication with this model, see [model configuration for Mistral](/docs/ai/models/mistral). ### MistralModelSettings **Bases:** [`ModelSettings`](/docs/ai/api/pydantic-ai/settings/#pydantic_ai.settings.ModelSettings) Settings used for a Mistral model request. ### MistralModel **Bases:** `Model[Mistral]` A model that uses Mistral. Internally, this uses the [Mistral Python client](https://github.com/mistralai/client-python) to interact with the API. [API Documentation](https://docs.mistral.ai/) #### Attributes ##### model\_name The model name. **Type:** `MistralModelName` ##### system The model provider. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) #### Methods ##### \_\_init\_\_ ```python def __init__( model_name: MistralModelName, provider: Literal['mistral'] | Provider[Mistral] = 'mistral', profile: ModelProfileSpec | None = None, json_mode_schema_prompt: str = 'Answer in JSON Object, respect the format:\n```\n{schema}\n```\n', settings: ModelSettings | None = None, ) ``` Initialize a Mistral model. ###### Parameters **`model_name`** : `MistralModelName` The name of the model to use. **`provider`** : [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['mistral'\] | `Provider`\[`Mistral`\] _Default:_ `'mistral'` The provider to use for authentication and API access. Can be either the string 'mistral' or an instance of `Provider[Mistral]`. If not provided, a new provider will be created using the other parameters. **`profile`** : `ModelProfileSpec` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` The model profile to use. Defaults to a profile picked by the provider based on the model name. **`json_mode_schema_prompt`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) _Default:_ `'Answer in JSON Object, respect the format:\n```\n{schema}\n```\n'` The prompt to show when the model expects a JSON object as input. **`settings`** : [`ModelSettings`](/docs/ai/api/pydantic-ai/settings/#pydantic_ai.settings.ModelSettings) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Model-specific settings that will be used as defaults for this model. ##### request `@async` ```python def request( messages: list[ModelMessage], model_settings: ModelSettings | None, model_request_parameters: ModelRequestParameters, ) -> ModelResponse ``` Make a non-streaming request to the model from Pydantic AI call. ###### Returns [`ModelResponse`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.ModelResponse) ##### request\_stream `@async` ```python def request_stream( messages: list[ModelMessage], model_settings: ModelSettings | None, model_request_parameters: ModelRequestParameters, run_context: RunContext[Any] | None = None, ) -> AsyncIterator[StreamedResponse] ``` Make a streaming request to the model from Pydantic AI call. ###### Returns [`AsyncIterator`](https://docs.python.org/3/library/typing.html#typing.AsyncIterator)\[`StreamedResponse`\] ### MistralStreamedResponse **Bases:** `StreamedResponse` Implementation of `StreamedResponse` for Mistral models. #### Attributes ##### model\_name Get the model name of the response. **Type:** `MistralModelName` ##### provider\_name Get the provider name. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### provider\_url Get the provider base URL. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### timestamp Get the timestamp of the response. **Type:** [`datetime`](https://docs.python.org/3/library/datetime.html#module-datetime) ### LatestMistralModelNames Latest Mistral models. **Default:** `Literal['mistral-large-latest', 'mistral-small-latest', 'codestral-latest', 'mistral-moderation-latest']` ### MistralModelName Possible Mistral model names. Since Mistral supports a variety of date-stamped models, we explicitly list the most popular models but allow any name in the type hints. Since [the Mistral docs](https://docs.mistral.ai/getting-started/models/models_overview/) for a full list. **Default:** `str | LatestMistralModelNames` --- # [pydantic_ai.models.ollama](https://pydantic.dev/docs/ai/api/models/ollama/) # pydantic\_ai.models.ollama ## Setup For details on how to set up authentication with this model, see [model configuration for Ollama](/docs/ai/models/ollama). Ollama model implementation using OpenAI-compatible API. ### OllamaModel **Bases:** `OpenAIChatModel` A model that uses Ollama's OpenAI-compatible Chat Completions API. Self-hosted Ollama (v0.5.0+) honors `response_format` with `json_schema` via `llama.cpp`'s grammar-constrained decoder, so `NativeOutput` produces schema-valid output at generation time. Ollama Cloud currently accepts `response_format` with `json_schema` without error but does not enforce the schema upstream (see [pydantic-ai#4917](https://github.com/pydantic/pydantic-ai/issues/4917) and [ollama/ollama#12362](https://github.com/ollama/ollama/issues/12362)). When this model detects a Cloud path -- either a `base_url` on `ollama.com` or a model name ending in `-cloud` -- it disables `supports_json_schema_output` on the resolved profile. With that flag off, [`NativeOutput`](/docs/ai/api/pydantic-ai/output/#pydantic_ai.output.NativeOutput) raises a clear [`UserError`](/docs/ai/api/pydantic-ai/exceptions/#pydantic_ai.exceptions.UserError) so users pick a mode that actually works on Cloud ([`ToolOutput`](/docs/ai/api/pydantic-ai/output/#pydantic_ai.output.ToolOutput) -- the default -- and [`PromptedOutput`](/docs/ai/api/pydantic-ai/output/#pydantic_ai.output.PromptedOutput) are both verified to work). Apart from `__init__`, all methods are inherited from the base class. #### Methods ##### \_\_init\_\_ ```python def __init__( model_name: str, provider: Literal['ollama'] | Provider[AsyncOpenAI] = 'ollama', profile: ModelProfileSpec | None = None, settings: ModelSettings | None = None, ) ``` Initialize an Ollama model. ###### Parameters **`model_name`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) The name of the Ollama model to use (e.g. `'qwen3'`, `'llama3.2'`). **`provider`** : [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['ollama'\] | `Provider`\[`AsyncOpenAI`\] _Default:_ `'ollama'` The provider to use. Defaults to `'ollama'`. **`profile`** : `ModelProfileSpec` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` The model profile to use. Defaults to a profile picked by the provider based on the model name, adjusted to disable `supports_json_schema_output` when the request routes through Ollama Cloud. **`settings`** : [`ModelSettings`](/docs/ai/api/pydantic-ai/settings/#pydantic_ai.settings.ModelSettings) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Model-specific settings that will be used as defaults for this model. --- # [pydantic_ai.models.openai](https://pydantic.dev/docs/ai/api/models/openai/) # pydantic\_ai.models.openai ## Setup For details on how to set up authentication with this model, see [model configuration for OpenAI](/docs/ai/models/openai). ### OpenAIChatModelSettings **Bases:** [`ModelSettings`](/docs/ai/api/pydantic-ai/settings/#pydantic_ai.settings.ModelSettings) Settings used for an OpenAI model request. #### Attributes ##### openai\_reasoning\_effort Constrains effort on reasoning for [reasoning models](https://platform.openai.com/docs/guides/reasoning). Currently supported values are `low`, `medium`, and `high`. Reducing reasoning effort can result in faster responses and fewer tokens used on reasoning in a response. **Type:** `ReasoningEffort` ##### openai\_logprobs Include log probabilities in the response. For Chat models, these will be included in `ModelResponse.provider_details['logprobs']`. For Responses models, these will be included in the response output parts `TextPart.provider_details['logprobs']`. **Type:** [`bool`](https://docs.python.org/3/library/functions.html#bool) ##### openai\_top\_logprobs Include log probabilities of the top n tokens in the response. **Type:** [`int`](https://docs.python.org/3/library/functions.html#int) ##### openai\_store Whether or not to store the output of this request in OpenAI's systems. If `False`, OpenAI will not store the request for its own internal review or training. See [OpenAI API reference](https://platform.openai.com/docs/api-reference/chat/create#chat-create-store). When used with `OpenAIResponsesModel`, stored responses appear in OpenAI's dashboard and can be referenced via [`openai_previous_response_id`](/docs/ai/api/models/openai/#pydantic_ai.models.openai.OpenAIResponsesModelSettings.openai_previous_response_id). Pair this with `openai_previous_response_id='auto'` to avoid storing duplicate copies of the conversation history across retries and subsequent requests within the same run. **Type:** [`bool`](https://docs.python.org/3/library/functions.html#bool) | [`None`](https://docs.python.org/3/library/constants.html#None) ##### openai\_user A unique identifier representing the end-user, which can help OpenAI monitor and detect abuse. See [OpenAI's safety best practices](https://platform.openai.com/docs/guides/safety-best-practices#end-user-ids) for more details. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### openai\_service\_tier The service tier to use for the model request. Currently supported values are `auto`, `default`, `flex`, and `priority`. For more information, see [OpenAI's service tiers documentation](https://platform.openai.com/docs/api-reference/chat/object#chat/object-service_tier). **Type:** [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['auto', 'default', 'flex', 'priority'\] ##### openai\_prediction Enables [predictive outputs](https://platform.openai.com/docs/guides/predicted-outputs). This feature is currently only supported for some OpenAI models. **Type:** `ChatCompletionPredictionContentParam` ##### openai\_prompt\_cache\_key Used by OpenAI to cache responses for similar requests to optimize your cache hit rates. See the [OpenAI Prompt Caching documentation](https://platform.openai.com/docs/guides/prompt-caching#how-it-works) for more information. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### openai\_prompt\_cache\_retention The retention policy for the prompt cache. Set to 24h to enable extended prompt caching, which keeps cached prefixes active for longer, up to a maximum of 24 hours. See the [OpenAI Prompt Caching documentation](https://platform.openai.com/docs/guides/prompt-caching#how-it-works) for more information. **Type:** [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['in\_memory', '24h'\] ##### openai\_continuous\_usage\_stats When True, enables continuous usage statistics in streaming responses. When enabled, the API returns cumulative usage data with each chunk rather than only at the end. This setting correctly handles the cumulative nature of these stats by using only the final usage values rather than summing all intermediate values. See [OpenAI's streaming documentation](https://platform.openai.com/docs/api-reference/chat/create#stream_options) for more information. **Type:** [`bool`](https://docs.python.org/3/library/functions.html#bool) ### OpenAIModelSettings **Bases:** `OpenAIChatModelSettings` Deprecated alias for `OpenAIChatModelSettings`. ### OpenAIResponsesModelSettings **Bases:** `OpenAIChatModelSettings` Settings used for an OpenAI Responses model request. ALL FIELDS MUST BE `openai_` PREFIXED SO YOU CAN MERGE THEM WITH OTHER MODELS. #### Attributes ##### openai\_native\_tools The provided OpenAI built-in tools to use. See [OpenAI's built-in tools](https://platform.openai.com/docs/guides/tools?api-mode=responses) for more details. **Type:** [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[`FileSearchToolParam` | `WebSearchToolParam` | `ComputerToolParam`\] ##### openai\_reasoning\_generate\_summary Deprecated alias for `openai_reasoning_summary`. **Type:** [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['detailed', 'concise'\] ##### openai\_reasoning\_summary A summary of the reasoning performed by the model. This can be useful for debugging and understanding the model's reasoning process. One of `concise`, `detailed`, or `auto`. Check the [OpenAI Reasoning documentation](https://platform.openai.com/docs/guides/reasoning?api-mode=responses#reasoning-summaries) for more details. **Type:** [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['detailed', 'concise', 'auto'\] ##### openai\_send\_reasoning\_ids Whether to send the unique IDs of reasoning, text, and function call parts from the message history to the model. Enabled by default for reasoning models. This can result in errors like `"Item 'rs_123' of type 'reasoning' was provided without its required following item."` if the message history you're sending does not match exactly what was received from the Responses API in a previous response, for example if you're using a [history processor](/docs/ai/core-concepts/message-history#processing-message-history). In that case, you'll want to disable this. **Type:** [`bool`](https://docs.python.org/3/library/functions.html#bool) ##### openai\_truncation The truncation strategy to use for the model response. It can be either: - `disabled` (default): If a model response will exceed the context window size for a model, the request will fail with a 400 error. - `auto`: If the context of this response and previous ones exceeds the model's context window size, the model will truncate the response to fit the context window by dropping input items in the middle of the conversation. **Type:** [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['disabled', 'auto'\] ##### openai\_text\_verbosity Constrains the verbosity of the model's text response. Lower values will result in more concise responses, while higher values will result in more verbose responses. Currently supported values are `low`, `medium`, and `high`. **Type:** [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['low', 'medium', 'high'\] ##### openai\_previous\_response\_id Reference a prior OpenAI response to continue a conversation server-side, omitting already-stored messages from the input. - `'auto'`: chain to the most recent `provider_response_id` in the message history. If the history contains no such response, no `previous_response_id` is sent. - A concrete response ID string: use it as the seed for the first request in the run (e.g. to continue from a prior turn). On subsequent in-run requests (retries, tool-call continuations), the most recent `provider_response_id` from the message history takes precedence so the chain extends correctly without re-sending messages that are already server-side. In both cases, messages that precede the chosen response in the history are omitted from the input, since OpenAI reconstructs them from server-side state. Requires the referenced response to have been stored (see `openai_store`, which defaults to `True` on OpenAI's side). Not compatible with Zero Data Retention. See the [OpenAI Responses API documentation](https://platform.openai.com/docs/guides/reasoning#keeping-reasoning-items-in-context) for more information. **Type:** [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['auto'\] | [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### openai\_conversation\_id Reference an OpenAI conversation to continue durable conversation state server-side. - `'auto'`: use the most recent OpenAI conversation ID from `ModelResponse.provider_details['conversation_id']` in the message history with the same Pydantic AI `conversation_id`, when available. If the history contains no such response, no `conversation` is sent. - A concrete conversation ID string: use it as the OpenAI Responses API `conversation` parameter. When a matching conversation ID is found in message history, messages that precede that response are omitted from the input, since OpenAI reconstructs them from the server-side conversation. Not compatible with [`openai_previous_response_id`](/docs/ai/api/models/openai/#pydantic_ai.models.openai.OpenAIResponsesModelSettings.openai_previous_response_id). See the [OpenAI conversation state documentation](https://platform.openai.com/docs/guides/conversation-state) for more information. **Type:** [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['auto'\] | [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### openai\_include\_code\_execution\_outputs Whether to include the code execution results in the response. Corresponds to the `code_interpreter_call.outputs` value of the `include` parameter in the Responses API. **Type:** [`bool`](https://docs.python.org/3/library/functions.html#bool) ##### openai\_include\_web\_search\_sources Whether to include the web search results in the response. Corresponds to the `web_search_call.action.sources` value of the `include` parameter in the Responses API. **Type:** [`bool`](https://docs.python.org/3/library/functions.html#bool) ##### openai\_include\_file\_search\_results Whether to include the file search results in the response. Corresponds to the `file_search_call.results` value of the `include` parameter in the Responses API. **Type:** [`bool`](https://docs.python.org/3/library/functions.html#bool) ##### openai\_include\_raw\_annotations Whether to include the raw annotations in `TextPart.provider_details`. When enabled, any annotations (e.g., citations from web search) will be available in the `provider_details['annotations']` field of text parts. This is opt-in since there may be overlap with native annotation support once added via [https://github.com/pydantic/pydantic-ai/issues/3126](https://github.com/pydantic/pydantic-ai/issues/3126). **Type:** [`bool`](https://docs.python.org/3/library/functions.html#bool) ##### openai\_context\_management Context management configuration for the request. This enables OpenAI's server-side automatic compaction inside the regular `/responses` call, as opposed to the standalone `/responses/compact` endpoint. See [OpenAI's compaction guide](https://developers.openai.com/api/docs/guides/compaction) for details. The [`OpenAICompaction`](/docs/ai/api/models/openai/#pydantic_ai.models.openai.OpenAICompaction) capability sets this automatically in its default (stateful) mode. **Type:** [`list`](https://docs.python.org/3/glossary.html#term-list)\[`ContextManagement`\] ### OpenAIChatModel **Bases:** `Model[AsyncOpenAI]` A model that uses the OpenAI API. Internally, this uses the [OpenAI Python client](https://github.com/openai/openai-python) to interact with the API. Apart from `__init__`, all methods are private or match those of the base class. #### Attributes ##### model\_name The model name. **Type:** `OpenAIModelName` ##### system The model provider. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### profile The model profile. WebSearchTool is only supported if openai\_chat\_supports\_web\_search is True. **Type:** [`ModelProfile`](/docs/ai/api/pydantic-ai/profiles/#pydantic_ai.profiles.ModelProfile) #### Methods ##### \_\_init\_\_ ```python def __init__( model_name: OpenAIModelName, provider: OpenAIChatCompatibleProvider | Literal['openai', 'openai-chat', 'gateway'] | Provider[AsyncOpenAI] = 'openai', profile: ModelProfileSpec | None = None, settings: ModelSettings | None = None, ) -> None def __init__( model_name: OpenAIModelName, provider: OpenAIChatCompatibleProvider | Literal['openai', 'openai-chat', 'gateway'] | Provider[AsyncOpenAI] = 'openai', profile: ModelProfileSpec | None = None, system_prompt_role: OpenAISystemPromptRole | None = None, settings: ModelSettings | None = None, ) -> None ``` Initialize an OpenAI model. ###### Parameters **`model_name`** : `OpenAIModelName` The name of the OpenAI model to use. List of model names available [here](https://github.com/openai/openai-python/blob/v1.54.3/src/openai/types/chat_model.py#L7) (Unfortunately, despite being ask to do so, OpenAI do not provide `.inv` files for their API). **`provider`** : `OpenAIChatCompatibleProvider` | [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['openai', 'openai-chat', 'gateway'\] | `Provider`\[`AsyncOpenAI`\] _Default:_ `'openai'` The provider to use. Defaults to `'openai'`. **`profile`** : `ModelProfileSpec` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` The model profile to use. Defaults to a profile picked by the provider based on the model name. **`system_prompt_role`** : `OpenAISystemPromptRole` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` The role to use for the system prompt message. If not provided, defaults to `'system'`. In the future, this may be inferred from the model name. **`settings`** : [`ModelSettings`](/docs/ai/api/pydantic-ai/settings/#pydantic_ai.settings.ModelSettings) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Default model settings for this model instance. ##### supported\_native\_tools `@classmethod` ```python def supported_native_tools(cls) -> frozenset[type[AbstractNativeTool]] ``` Return the set of builtin tool types this model can handle. ###### Returns [`frozenset`](https://docs.python.org/3/library/stdtypes.html#frozenset)\[[`type`](https://docs.python.org/3/glossary.html#term-type)\[`AbstractNativeTool`\]\] ### OpenAIModel **Bases:** `OpenAIChatModel` Deprecated alias for `OpenAIChatModel`. ### OpenAIResponsesModel **Bases:** `Model[AsyncOpenAI]` A model that uses the OpenAI Responses API. The [OpenAI Responses API](https://platform.openai.com/docs/api-reference/responses) is the new API for OpenAI models. If you are interested in the differences between the Responses API and the Chat Completions API, see the [OpenAI API docs](https://platform.openai.com/docs/guides/responses-vs-chat-completions). #### Attributes ##### model\_name The model name. **Type:** `OpenAIModelName` ##### system The model provider. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) #### Methods ##### \_\_init\_\_ ```python def __init__( model_name: OpenAIModelName, provider: OpenAIResponsesCompatibleProvider | Literal['openai', 'gateway'] | Provider[AsyncOpenAI] = 'openai', profile: ModelProfileSpec | None = None, settings: ModelSettings | None = None, ) ``` Initialize an OpenAI Responses model. ###### Parameters **`model_name`** : `OpenAIModelName` The name of the OpenAI model to use. **`provider`** : `OpenAIResponsesCompatibleProvider` | [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['openai', 'gateway'\] | `Provider`\[`AsyncOpenAI`\] _Default:_ `'openai'` The provider to use. Defaults to `'openai'`. **`profile`** : `ModelProfileSpec` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` The model profile to use. Defaults to a profile picked by the provider based on the model name. **`settings`** : [`ModelSettings`](/docs/ai/api/pydantic-ai/settings/#pydantic_ai.settings.ModelSettings) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Default model settings for this model instance. ##### supported\_native\_tools `@classmethod` ```python def supported_native_tools(cls) -> frozenset[type[AbstractNativeTool]] ``` Return the set of builtin tool types this model can handle. ###### Returns [`frozenset`](https://docs.python.org/3/library/stdtypes.html#frozenset)\[[`type`](https://docs.python.org/3/glossary.html#term-type)\[`AbstractNativeTool`\]\] ##### compact\_messages `@async` ```python def compact_messages( request_context: ModelRequestContext, instructions: str | None = None, ) -> ModelResponse ``` Compact messages using the OpenAI Responses compaction endpoint. This calls OpenAI's `responses.compact` API to produce an encrypted compaction that summarizes the conversation history. The returned `ModelResponse` contains a single `CompactionPart` that must be round-tripped in subsequent requests. ###### Returns [`ModelResponse`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.ModelResponse) -- A `ModelResponse` with a single `CompactionPart` containing the encrypted compaction data. ###### Parameters **`request_context`** : `ModelRequestContext` The model request context containing messages, settings, and parameters. **`instructions`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional custom instructions for the compaction summarization. If provided, these override the agent-level instructions. ### OpenAIStreamedResponse **Bases:** `StreamedResponse` Implementation of `StreamedResponse` for OpenAI models. #### Attributes ##### model\_name Get the model name of the response. **Type:** `OpenAIModelName` ##### provider\_name Get the provider name. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### provider\_url Get the provider base URL. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### timestamp Get the timestamp of the response. **Type:** [`datetime`](https://docs.python.org/3/library/datetime.html#module-datetime) ### OpenAIResponsesStreamedResponse **Bases:** `StreamedResponse` Implementation of `StreamedResponse` for OpenAI Responses API. #### Attributes ##### model\_name Get the model name of the response. **Type:** `OpenAIModelName` ##### provider\_name Get the provider name. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### provider\_url Get the provider base URL. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### timestamp Get the timestamp of the response. **Type:** [`datetime`](https://docs.python.org/3/library/datetime.html#module-datetime) ### OpenAICompaction **Bases:** `AbstractCapability[AgentDepsT]` Compaction capability for OpenAI Responses API. Automatically compacts conversation history to keep long-running agent runs within manageable context limits. Two modes are supported, selected by the `stateless` flag: - **Stateful mode** (default, `stateless=False`): configures [OpenAI's server-side auto-compaction](https://developers.openai.com/api/docs/guides/compaction) via the `context_management` field on the regular `/responses` request. The server triggers compaction when input tokens cross a threshold, and the compacted item is returned alongside the normal response. Compatible with [`openai_previous_response_id='auto'`](/docs/ai/api/models/openai/#pydantic_ai.models.openai.OpenAIResponsesModelSettings.openai_previous_response_id) and server-side conversation state. Configurable with `token_threshold` (`compact_threshold` on the API). If omitted, OpenAI picks a server-side default. - **Stateless mode** (`stateless=True`): calls the stateless `/responses/compact` endpoint from a `before_model_request` hook when your trigger condition is met. Use this in [ZDR](https://openai.com/enterprise-privacy/) environments where OpenAI must not retain conversation data, when you set `openai_store=False`, or when you need explicit out-of-band control over when compaction runs. Requires either `message_count_threshold` or a custom `trigger` callable. If `stateless` is not set, it is inferred from which parameters you provide: passing any stateless-only parameter (`message_count_threshold` or `trigger`) implies `stateless=True`; otherwise stateful mode is used. Example usage: ```python from pydantic_ai import Agent from pydantic_ai.models.openai import OpenAICompaction # Stateful mode with OpenAI's server-side default threshold: agent = Agent( 'openai-responses:gpt-5.2', capabilities=[OpenAICompaction()], ) # Stateful mode with a custom token threshold: agent = Agent( 'openai-responses:gpt-5.2', capabilities=[OpenAICompaction(token_threshold=100_000)], ) # Stateless mode for ZDR environments or explicit control: agent = Agent( 'openai-responses:gpt-5.2', capabilities=[OpenAICompaction(message_count_threshold=20)], ) ``` #### Methods ##### \_\_init\_\_ ```python def __init__( stateless: bool | None = None, token_threshold: int | None = None, message_count_threshold: int | None = None, trigger: Callable[[list[ModelMessage]], bool] | None = None, instructions: str | None = None, ) -> None ``` Initialize the OpenAI compaction capability. ###### Returns [`None`](https://docs.python.org/3/library/constants.html#None) ###### Parameters **`stateless`** : [`bool`](https://docs.python.org/3/library/functions.html#bool) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Select the compaction mode explicitly. If `None` (the default), the mode is inferred from the other parameters: passing any stateless-only parameter (`message_count_threshold` or `trigger`) implies `stateless=True`; otherwise stateful mode is used. **`token_threshold`** : [`int`](https://docs.python.org/3/library/functions.html#int) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Stateful-mode only. Input token threshold at which OpenAI's server-side compaction is triggered. Corresponds to `compact_threshold` in the `context_management` API field. If `None`, OpenAI picks a server-side default. **`message_count_threshold`** : [`int`](https://docs.python.org/3/library/functions.html#int) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Stateless-mode only. Compact when the message count exceeds this threshold. **`trigger`** : [`Callable`](https://docs.python.org/3/library/typing.html#typing.Callable)\[\[[`list`](https://docs.python.org/3/glossary.html#term-list)\[[`ModelMessage`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.ModelMessage)\]\], [`bool`](https://docs.python.org/3/library/functions.html#bool)\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Stateless-mode only. Custom callable that decides whether to compact based on the current messages. Takes precedence over `message_count_threshold`. **`instructions`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Deprecated. OpenAI's `/compact` endpoint treats `instructions` as a system/developer message inserted into the compaction model's context, not as a directive for how to summarize the conversation. This does not match [`AnthropicCompaction.instructions`](/docs/ai/api/models/anthropic/#pydantic_ai.models.anthropic.AnthropicCompaction) semantics, so the field is deprecated and will be removed in a future version. ### DEPRECATED\_OPENAI\_MODELS Models that are deprecated or don't exist but are still present in the OpenAI SDK's type definitions. **Type:** [`frozenset`](https://docs.python.org/3/library/stdtypes.html#frozenset)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str)\] **Default:** `frozenset({'chatgpt-4o-latest', 'codex-mini-latest', 'gpt-4-0125-preview', 'gpt-4-1106-preview', 'gpt-4-turbo-preview', 'gpt-4-32k', 'gpt-4-32k-0314', 'gpt-4-32k-0613', 'gpt-4-vision-preview', 'gpt-4o-audio-preview-2024-10-01', 'gpt-5.1-mini', 'o1-mini', 'o1-mini-2024-09-12', 'o1-preview', 'o1-preview-2024-09-12'})` ### OpenAIModelName Possible OpenAI model names. Since OpenAI supports a variety of date-stamped models, we explicitly list the latest models but allow any name in the type hints. See [the OpenAI docs](https://platform.openai.com/docs/models) for a full list. Using this more broad type for the model name instead of the ChatModel definition allows this model to be used more easily with other model types (ie, Ollama, Deepseek). **Default:** `str | AllModels` ### MCP\_SERVER\_TOOL\_CONNECTOR\_URI\_SCHEME Prefix for OpenAI connector IDs. OpenAI supports either a URL or a connector ID when passing MCP configuration to a model, by using that prefix like `x-openai-connector:` in a URL, you can pass a connector ID to a model. **Type:** [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['x-openai-connector'\] **Default:** `'x-openai-connector'` --- # [pydantic_ai.models.openrouter](https://pydantic.dev/docs/ai/api/models/openrouter/) # pydantic\_ai.models.openrouter ## Setup For details on how to set up authentication with this model, see [model configuration for OpenRouter](/docs/ai/models/openrouter). ### OpenRouterProviderConfig **Bases:** [`TypedDict`](https://docs.python.org/3/library/typing.html#typing.TypedDict) Represents the 'Provider' object from the OpenRouter API. #### Attributes ##### order List of provider slugs to try in order (e.g. \["anthropic", "openai"\]). [See details](https://openrouter.ai/docs/features/provider-routing#ordering-specific-providers) **Type:** [`list`](https://docs.python.org/3/glossary.html#term-list)\[`OpenRouterProviderName`\] ##### allow\_fallbacks Whether to allow backup providers when the primary is unavailable. [See details](https://openrouter.ai/docs/features/provider-routing#disabling-fallbacks) **Type:** [`bool`](https://docs.python.org/3/library/functions.html#bool) ##### require\_parameters Only use providers that support all parameters in your request. **Type:** [`bool`](https://docs.python.org/3/library/functions.html#bool) ##### data\_collection Control whether to use providers that may store data. [See details](https://openrouter.ai/docs/features/provider-routing#requiring-providers-to-comply-with-data-policies) **Type:** [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['allow', 'deny'\] ##### zdr Restrict routing to only ZDR (Zero Data Retention) endpoints. [See details](https://openrouter.ai/docs/features/provider-routing#zero-data-retention-enforcement) **Type:** [`bool`](https://docs.python.org/3/library/functions.html#bool) ##### only List of provider slugs to allow for this request. [See details](https://openrouter.ai/docs/features/provider-routing#allowing-only-specific-providers) **Type:** [`list`](https://docs.python.org/3/glossary.html#term-list)\[`OpenRouterProviderName`\] ##### ignore List of provider slugs to skip for this request. [See details](https://openrouter.ai/docs/features/provider-routing#ignoring-providers) **Type:** [`list`](https://docs.python.org/3/glossary.html#term-list)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str)\] ##### quantizations List of quantization levels to filter by (e.g. \["int4", "int8"\]). [See details](https://openrouter.ai/docs/features/provider-routing#quantization) **Type:** [`list`](https://docs.python.org/3/glossary.html#term-list)\[[`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['int4', 'int8', 'fp4', 'fp6', 'fp8', 'fp16', 'bf16', 'fp32', 'unknown'\]\] ##### sort Sort providers by price or throughput. (e.g. "price" or "throughput"). [See details](https://openrouter.ai/docs/features/provider-routing#provider-sorting) **Type:** [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['price', 'throughput', 'latency'\] ##### max\_price The maximum pricing you want to pay for this request. [See details](https://openrouter.ai/docs/features/provider-routing#max-price) **Type:** `_OpenRouterMaxPrice` ### OpenRouterReasoning **Bases:** [`TypedDict`](https://docs.python.org/3/library/typing.html#typing.TypedDict) Configuration for reasoning tokens in OpenRouter requests. Reasoning tokens allow models to show their step-by-step thinking process. You can configure this using either OpenAI-style effort levels or Anthropic-style token limits, but not both simultaneously. #### Attributes ##### effort OpenAI-style reasoning effort level. Cannot be used with max\_tokens. **Type:** [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['xhigh', 'high', 'medium', 'low', 'minimal', 'none'\] ##### max\_tokens Anthropic-style specific token limit for reasoning. Cannot be used with effort. **Type:** [`int`](https://docs.python.org/3/library/functions.html#int) ##### exclude Whether to exclude reasoning tokens from the response. Default is False. All models support this. **Type:** [`bool`](https://docs.python.org/3/library/functions.html#bool) ##### enabled Whether to enable reasoning with default parameters. Default is inferred from effort or max\_tokens. **Type:** [`bool`](https://docs.python.org/3/library/functions.html#bool) ### OpenRouterUsageConfig **Bases:** [`TypedDict`](https://docs.python.org/3/library/typing.html#typing.TypedDict) Configuration for OpenRouter usage. ### OpenRouterModelSettings **Bases:** [`ModelSettings`](/docs/ai/api/pydantic-ai/settings/#pydantic_ai.settings.ModelSettings) Settings used for an OpenRouter model request. #### Attributes ##### openrouter\_models A list of fallback models. These models will be tried, in order, if the main model returns an error. [See details](https://openrouter.ai/docs/features/model-routing#the-models-parameter) **Type:** [`list`](https://docs.python.org/3/glossary.html#term-list)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str)\] ##### openrouter\_provider OpenRouter routes requests to the best available providers for your model. By default, requests are load balanced across the top providers to maximize uptime. You can customize how your requests are routed using the provider object. [See more](https://openrouter.ai/docs/features/provider-routing) **Type:** `OpenRouterProviderConfig` ##### openrouter\_preset Presets allow you to separate your LLM configuration from your code. Create and manage presets through the OpenRouter web application to control provider routing, model selection, system prompts, and other parameters, then reference them in OpenRouter API requests. [See more](https://openrouter.ai/docs/features/presets) **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### openrouter\_transforms To help with prompts that exceed the maximum context size of a model. Transforms work by removing or truncating messages from the middle of the prompt, until the prompt fits within the model's context window. [See more](https://openrouter.ai/docs/features/message-transforms) **Type:** [`list`](https://docs.python.org/3/glossary.html#term-list)\[`OpenRouterTransforms`\] ##### openrouter\_reasoning To control the reasoning tokens in the request. The reasoning config object consolidates settings for controlling reasoning strength across different models. [See more](https://openrouter.ai/docs/use-cases/reasoning-tokens) **Type:** `OpenRouterReasoning` ##### openrouter\_usage To control the usage of the model. The usage config object consolidates settings for enabling detailed usage information. [See more](https://openrouter.ai/docs/use-cases/usage-accounting) **Type:** `OpenRouterUsageConfig` ### OpenRouterModel **Bases:** `OpenAIChatModel` Extends OpenAIModel to capture extra metadata for Openrouter. #### Methods ##### \_\_init\_\_ ```python def __init__( model_name: str, provider: Literal['openrouter'] | Provider[AsyncOpenAI] = 'openrouter', profile: ModelProfileSpec | None = None, settings: ModelSettings | None = None, ) ``` Initialize an OpenRouter model. ###### Parameters **`model_name`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) The name of the model to use. **`provider`** : [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['openrouter'\] | `Provider`\[`AsyncOpenAI`\] _Default:_ `'openrouter'` The provider to use for authentication and API access. If not provided, a new provider will be created with the default settings. **`profile`** : `ModelProfileSpec` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` The model profile to use. Defaults to a profile picked by the provider based on the model name. **`settings`** : [`ModelSettings`](/docs/ai/api/pydantic-ai/settings/#pydantic_ai.settings.ModelSettings) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Model-specific settings that will be used as defaults for this model. ##### supported\_native\_tools `@classmethod` ```python def supported_native_tools(cls) -> frozenset[type[AbstractNativeTool]] ``` Return the set of builtin tool types this model can handle. OpenRouter supports web search via its plugins system. ###### Returns [`frozenset`](https://docs.python.org/3/library/stdtypes.html#frozenset)\[[`type`](https://docs.python.org/3/glossary.html#term-type)\[`AbstractNativeTool`\]\] ### OpenRouterStreamedResponse **Bases:** `OpenAIStreamedResponse` Implementation of `StreamedResponse` for OpenRouter models. ### KnownOpenRouterProviders Known providers in the OpenRouter marketplace **Default:** `Literal['z-ai', 'cerebras', 'venice', 'moonshotai', 'morph', 'stealth', 'wandb', 'klusterai', 'openai', 'sambanova', 'amazon-bedrock', 'mistral', 'nextbit', 'atoma', 'ai21', 'minimax', 'baseten', 'anthropic', 'featherless', 'groq', 'lambda', 'azure', 'ncompass', 'deepseek', 'hyperbolic', 'crusoe', 'cohere', 'mancer', 'avian', 'perplexity', 'novita', 'siliconflow', 'switchpoint', 'xai', 'inflection', 'fireworks', 'deepinfra', 'inference-net', 'inception', 'atlas-cloud', 'nvidia', 'alibaba', 'friendli', 'infermatic', 'targon', 'ubicloud', 'aion-labs', 'liquid', 'nineteen', 'cloudflare', 'nebius', 'chutes', 'enfer', 'crofai', 'open-inference', 'phala', 'gmicloud', 'meta', 'relace', 'parasail', 'together', 'google-ai-studio', 'google-vertex']` ### OpenRouterProviderName Possible OpenRouter provider names. Since OpenRouter is constantly updating their list of providers, we explicitly list some known providers but allow any name in the type hints. See [the OpenRouter API](https://openrouter.ai/docs/api-reference/list-available-providers) for a full list. **Default:** `str | KnownOpenRouterProviders` ### OpenRouterTransforms Available messages transforms for OpenRouter models with limited token windows. Currently only supports 'middle-out', but is expected to grow in the future. **Default:** `Literal['middle-out']` --- # [pydantic_ai.models.outlines](https://pydantic.dev/docs/ai/api/models/outlines/) # pydantic\_ai.models.outlines ## Setup For details on how to set up this model, see [model configuration for Outlines](/docs/ai/models/outlines). ### OutlinesModel **Bases:** `Model` A model that relies on the Outlines library to run non API-based models. #### Methods ##### \_\_init\_\_ ```python def __init__( model: OutlinesBaseModel | OutlinesAsyncBaseModel, provider: Literal['outlines'] | Provider[OutlinesBaseModel] = 'outlines', profile: ModelProfileSpec | None = None, settings: ModelSettings | None = None, ) ``` Initialize an Outlines model. ###### Parameters **`model`** : `OutlinesBaseModel` | `OutlinesAsyncBaseModel` The Outlines model used for the model. **`provider`** : [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['outlines'\] | `Provider`\[`OutlinesBaseModel`\] _Default:_ `'outlines'` The provider to use for OutlinesModel. Can be either the string 'outlines' or an instance of `Provider[OutlinesBaseModel]`. If not provided, the other parameters will be used. **`profile`** : `ModelProfileSpec` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` The model profile to use. Defaults to a profile picked by the provider. **`settings`** : [`ModelSettings`](/docs/ai/api/pydantic-ai/settings/#pydantic_ai.settings.ModelSettings) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Default model settings for this model instance. ##### from\_transformers `@classmethod` ```python def from_transformers( cls, hf_model: transformers.modeling_utils.PreTrainedModel, hf_tokenizer_or_processor: transformers.PreTrainedTokenizer | transformers.processing_utils.ProcessorMixin, provider: Literal['outlines'] | Provider[OutlinesBaseModel] = 'outlines', profile: ModelProfileSpec | None = None, settings: ModelSettings | None = None, ) ``` Create an Outlines model from a Hugging Face model and tokenizer. ###### Parameters **`hf_model`** : `transformers.modeling_utils.PreTrainedModel` The Hugging Face PreTrainedModel or any model that is compatible with the `transformers` API. **`hf_tokenizer_or_processor`** : `transformers.PreTrainedTokenizer` | `transformers.processing_utils.ProcessorMixin` Either a HuggingFace `PreTrainedTokenizer` or any tokenizer that is compatible with the `transformers` API, or a HuggingFace processor inheriting from `ProcessorMixin`. If a tokenizer is provided, a regular model will be used, while if you provide a processor, it will be a multimodal model. **`provider`** : [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['outlines'\] | `Provider`\[`OutlinesBaseModel`\] _Default:_ `'outlines'` The provider to use for OutlinesModel. Can be either the string 'outlines' or an instance of `Provider[OutlinesBaseModel]`. If not provided, the other parameters will be used. **`profile`** : `ModelProfileSpec` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` The model profile to use. Defaults to a profile picked by the provider. **`settings`** : [`ModelSettings`](/docs/ai/api/pydantic-ai/settings/#pydantic_ai.settings.ModelSettings) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Default model settings for this model instance. ##### from\_llamacpp `@classmethod` ```python def from_llamacpp( cls, llama_model: llama_cpp.Llama, provider: Literal['outlines'] | Provider[OutlinesBaseModel] = 'outlines', profile: ModelProfileSpec | None = None, settings: ModelSettings | None = None, ) ``` Create an Outlines model from a LlamaCpp model. ###### Parameters **`llama_model`** : `llama_cpp.Llama` The llama\_cpp.Llama model to use. **`provider`** : [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['outlines'\] | `Provider`\[`OutlinesBaseModel`\] _Default:_ `'outlines'` The provider to use for OutlinesModel. Can be either the string 'outlines' or an instance of `Provider[OutlinesBaseModel]`. If not provided, the other parameters will be used. **`profile`** : `ModelProfileSpec` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` The model profile to use. Defaults to a profile picked by the provider. **`settings`** : [`ModelSettings`](/docs/ai/api/pydantic-ai/settings/#pydantic_ai.settings.ModelSettings) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Default model settings for this model instance. ##### from\_mlxlm `@classmethod` ```python def from_mlxlm( cls, mlx_model: nn.Module, mlx_tokenizer: transformers.PreTrainedTokenizer, provider: Literal['outlines'] | Provider[OutlinesBaseModel] = 'outlines', profile: ModelProfileSpec | None = None, settings: ModelSettings | None = None, ) ``` Create an Outlines model from a MLXLM model. ###### Parameters **`mlx_model`** : `nn.Module` The nn.Module model to use. **`mlx_tokenizer`** : `transformers.PreTrainedTokenizer` The PreTrainedTokenizer to use. **`provider`** : [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['outlines'\] | `Provider`\[`OutlinesBaseModel`\] _Default:_ `'outlines'` The provider to use for OutlinesModel. Can be either the string 'outlines' or an instance of `Provider[OutlinesBaseModel]`. If not provided, the other parameters will be used. **`profile`** : `ModelProfileSpec` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` The model profile to use. Defaults to a profile picked by the provider. **`settings`** : [`ModelSettings`](/docs/ai/api/pydantic-ai/settings/#pydantic_ai.settings.ModelSettings) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Default model settings for this model instance. ##### from\_sglang `@classmethod` ```python def from_sglang( cls, base_url: str, api_key: str | None = None, model_name: str | None = None, provider: Literal['outlines'] | Provider[OutlinesBaseModel] = 'outlines', profile: ModelProfileSpec | None = None, settings: ModelSettings | None = None, ) ``` Create an Outlines model to send requests to an SGLang server. ###### Parameters **`base_url`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) The url of the SGLang server. **`api_key`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` The API key to use for authenticating requests to the SGLang server. **`model_name`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` The name of the model to use. **`provider`** : [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['outlines'\] | `Provider`\[`OutlinesBaseModel`\] _Default:_ `'outlines'` The provider to use for OutlinesModel. Can be either the string 'outlines' or an instance of `Provider[OutlinesBaseModel]`. If not provided, the other parameters will be used. **`profile`** : `ModelProfileSpec` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` The model profile to use. Defaults to a profile picked by the provider. **`settings`** : [`ModelSettings`](/docs/ai/api/pydantic-ai/settings/#pydantic_ai.settings.ModelSettings) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Default model settings for this model instance. ##### from\_vllm\_offline `@classmethod` ```python def from_vllm_offline( cls, vllm_model: Any, provider: Literal['outlines'] | Provider[OutlinesBaseModel] = 'outlines', profile: ModelProfileSpec | None = None, settings: ModelSettings | None = None, ) ``` Create an Outlines model from a vLLM offline inference model. ###### Parameters **`vllm_model`** : [`Any`](https://docs.python.org/3/library/typing.html#typing.Any) The vllm.LLM local model to use. **`provider`** : [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['outlines'\] | `Provider`\[`OutlinesBaseModel`\] _Default:_ `'outlines'` The provider to use for OutlinesModel. Can be either the string 'outlines' or an instance of `Provider[OutlinesBaseModel]`. If not provided, the other parameters will be used. **`profile`** : `ModelProfileSpec` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` The model profile to use. Defaults to a profile picked by the provider. **`settings`** : [`ModelSettings`](/docs/ai/api/pydantic-ai/settings/#pydantic_ai.settings.ModelSettings) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Default model settings for this model instance. ##### format\_inference\_kwargs ```python def format_inference_kwargs(model_settings: ModelSettings | None) -> dict[str, Any] ``` Format the model settings for the inference kwargs. ###### Returns [`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] ### OutlinesStreamedResponse **Bases:** `StreamedResponse` Implementation of `StreamedResponse` for Outlines models. #### Attributes ##### model\_name Get the model name of the response. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### provider\_name Get the provider name. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### provider\_url Get the provider base URL. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) ##### timestamp Get the timestamp of the response. **Type:** [`datetime`](https://docs.python.org/3/library/datetime.html#module-datetime) --- # [pydantic_ai.models.test](https://pydantic.dev/docs/ai/api/models/test/) # pydantic\_ai.models.test Utility model for quickly testing apps built with Pydantic AI. Here's a minimal example: test\_model\_usage.py ```python from pydantic_ai import Agent from pydantic_ai.models.test import TestModel my_agent = Agent('openai:gpt-5.2', instructions='...') async def test_my_agent(): """Unit test for my_agent, to be run by pytest.""" m = TestModel() with my_agent.override(model=m): result = await my_agent.run('Testing my agent...') assert result.output == 'success (no tool calls)' assert m.last_model_request_parameters.function_tools == [] ``` See [Unit testing with `TestModel`](/docs/ai/guides/testing#unit-testing-with-testmodel) for detailed documentation. ### TestModel **Bases:** `Model` A model specifically for testing purposes. This will (by default) call all tools in the agent, then return a tool response if possible, otherwise a plain response. How useful this model is will vary significantly. Apart from `__init__` derived by the `dataclass` decorator, all methods are private or match those of the base class. #### Attributes ##### call\_tools List of tools to call. If `'all'`, all tools will be called. **Type:** [`list`](https://docs.python.org/3/glossary.html#term-list)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str)\] | [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['all'\] **Default:** `call_tools` ##### custom\_output\_text If set, this text is returned as the final output. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `custom_output_text` ##### custom\_output\_args If set, these args will be passed to the output tool. **Type:** [`Any`](https://docs.python.org/3/library/typing.html#typing.Any) | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `custom_output_args` ##### seed Seed for generating random data. **Type:** [`int`](https://docs.python.org/3/library/functions.html#int) **Default:** `seed` ##### last\_model\_request\_parameters The last ModelRequestParameters passed to the model in a request. The ModelRequestParameters contains information about the function and output tools available during request handling. This is set when a request is made, so will reflect the function tools from the last step of the last run. **Type:** `ModelRequestParameters` | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `None` ##### model\_name The model name. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### system The model provider. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) #### Methods ##### \_\_init\_\_ ```python def __init__( call_tools: list[str] | Literal['all'] = 'all', custom_output_text: str | None = None, custom_output_args: Any | None = None, seed: int = 0, model_name: str = 'test', profile: ModelProfileSpec | None = None, settings: ModelSettings | None = None, ) ``` Initialize TestModel with optional settings and profile. ##### supported\_native\_tools `@classmethod` ```python def supported_native_tools(cls) -> frozenset[type[AbstractNativeTool]] ``` TestModel supports all native tools for testing flexibility. `ToolSearchTool` is excluded because TestModel can't emulate provider-native tool search. Auto-injected `ToolSearch` capabilities work transparently thanks to the local `search_tools` fallback. ###### Returns [`frozenset`](https://docs.python.org/3/library/stdtypes.html#frozenset)\[[`type`](https://docs.python.org/3/glossary.html#term-type)\[`AbstractNativeTool`\]\] ### TestStreamedResponse **Bases:** `StreamedResponse` A structured response that streams test data. #### Attributes ##### model\_name Get the model name of the response. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### provider\_name Get the provider name. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### provider\_url Get the provider base URL. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) ##### timestamp Get the timestamp of the response. **Type:** [`datetime`](https://docs.python.org/3/library/datetime.html#module-datetime) --- # [pydantic_ai.models.wrapper](https://pydantic.dev/docs/ai/api/models/wrapper/) # pydantic\_ai.models.wrapper ### CompletedStreamedResponse **Bases:** `StreamedResponse` A `StreamedResponse` that wraps an already-completed `ModelResponse`. Used by durable execution integrations (Temporal, Prefect, DBOS) where the actual stream is consumed within a task/activity and only the final response is returned. ### WrapperModel **Bases:** `Model` Model which wraps another model. Does nothing on its own, used as a base class. #### Attributes ##### wrapped The underlying model being wrapped. **Type:** `Model` **Default:** `infer_model(wrapped)` ##### settings Get the settings from the wrapped model. **Type:** [`ModelSettings`](/docs/ai/api/pydantic-ai/settings/#pydantic_ai.settings.ModelSettings) | [`None`](https://docs.python.org/3/library/constants.html#None) --- # [pydantic_ai.models.xai](https://pydantic.dev/docs/ai/api/models/xai/) # pydantic\_ai.models.xai ## Setup For details on how to set up authentication with this model, see [model configuration for xAI](/docs/ai/models/xai). xAI model implementation using [xAI SDK](https://github.com/xai-org/xai-sdk-python). ### XaiModelSettings **Bases:** [`ModelSettings`](/docs/ai/api/pydantic-ai/settings/#pydantic_ai.settings.ModelSettings) Settings specific to xAI models. See [xAI SDK documentation](https://docs.x.ai/docs) for more details on these parameters. #### Attributes ##### xai\_logprobs Whether to return log probabilities of the output tokens or not. **Type:** [`bool`](https://docs.python.org/3/library/functions.html#bool) ##### xai\_top\_logprobs An integer between 0 and 20 specifying the number of most likely tokens to return at each position. **Type:** [`int`](https://docs.python.org/3/library/functions.html#int) ##### xai\_user A unique identifier representing your end-user, which can help xAI to monitor and detect abuse. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### xai\_store\_messages Whether to store messages on xAI's servers for conversation continuity. **Type:** [`bool`](https://docs.python.org/3/library/functions.html#bool) ##### xai\_previous\_response\_id The ID of the previous response to continue the conversation. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### xai\_include\_encrypted\_content Whether to include the encrypted content in the response. Corresponds to the `use_encrypted_content` value of the model settings in the Responses API. **Type:** [`bool`](https://docs.python.org/3/library/functions.html#bool) ##### xai\_include\_code\_execution\_output Whether to include the code execution results in the response. Corresponds to the `code_interpreter_call.outputs` value of the `include` parameter in the Responses API. **Type:** [`bool`](https://docs.python.org/3/library/functions.html#bool) ##### xai\_include\_web\_search\_output Whether to include the web search results in the response. Corresponds to the `web_search_call.action.sources` value of the `include` parameter in the Responses API. **Type:** [`bool`](https://docs.python.org/3/library/functions.html#bool) ##### xai\_include\_inline\_citations Whether to include inline citations in the response. Corresponds to the `inline_citations` option in the xAI `include` parameter. **Type:** [`bool`](https://docs.python.org/3/library/functions.html#bool) ##### xai\_include\_mcp\_output Whether to include the MCP results in the response. Corresponds to the `mcp_call.outputs` value of the `include` parameter in the Responses API. **Type:** [`bool`](https://docs.python.org/3/library/functions.html#bool) ##### xai\_include\_x\_search\_output Whether to include the X search results in the response. Corresponds to the `x_search_call.outputs` value of the `include` parameter in the Responses API. **Type:** [`bool`](https://docs.python.org/3/library/functions.html#bool) ##### xai\_include\_collections\_search\_output Whether to include the collections search results in the response. Corresponds to the `collections_search_call.outputs` value of the `include` parameter in the Responses API. **Type:** [`bool`](https://docs.python.org/3/library/functions.html#bool) ##### xai\_reasoning\_effort Reasoning effort level for Grok reasoning models. See [https://docs.x.ai](https://docs.x.ai) for details. **Type:** [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['low', 'high'\] ### XSearch **Bases:** `NativeOrLocalTool[AgentDepsT]` X (Twitter) search capability for xAI models. Uses the xAI model's native x\_search builtin tool. Only works with xAI models. #### Attributes ##### allowed\_x\_handles If provided, only posts from these X handles will be included (max 10). Requires builtin support. **Type:** [`list`](https://docs.python.org/3/glossary.html#term-list)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str)\] | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `allowed_x_handles` ##### excluded\_x\_handles If provided, posts from these X handles will be excluded (max 10). Requires builtin support. **Type:** [`list`](https://docs.python.org/3/glossary.html#term-list)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str)\] | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `excluded_x_handles` ##### from\_date If provided, only posts created on or after this datetime will be included. **Type:** [`datetime`](https://docs.python.org/3/library/datetime.html#module-datetime) | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `from_date` ##### to\_date If provided, only posts created on or before this datetime will be included. **Type:** [`datetime`](https://docs.python.org/3/library/datetime.html#module-datetime) | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `to_date` ##### enable\_image\_understanding Enable image analysis from X posts. Defaults to `False`. **Type:** [`bool`](https://docs.python.org/3/library/functions.html#bool) **Default:** `enable_image_understanding` ##### enable\_video\_understanding Enable video analysis from X content. Defaults to `False`. **Type:** [`bool`](https://docs.python.org/3/library/functions.html#bool) **Default:** `enable_video_understanding` ##### include\_output Include raw X search results in the response as [`NativeToolReturnPart`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.NativeToolReturnPart). Defaults to `False`. **Type:** [`bool`](https://docs.python.org/3/library/functions.html#bool) **Default:** `include_output` ### XaiModel **Bases:** `Model[AsyncClient]` A model that uses the xAI SDK to interact with xAI models. #### Attributes ##### model\_name The model name. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### system The model provider. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) #### Methods ##### \_\_init\_\_ ```python def __init__( model_name: XaiModelName, provider: Literal['xai'] | Provider[AsyncClient] = 'xai', profile: ModelProfileSpec | None = None, settings: ModelSettings | None = None, ) ``` Initialize the xAI model. ###### Parameters **`model_name`** : `XaiModelName` The name of the xAI model to use (e.g., "grok-4-1-fast-non-reasoning") **`provider`** : [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['xai'\] | `Provider`\[`AsyncClient`\] _Default:_ `'xai'` The provider to use for API calls. Defaults to `'xai'`. **`profile`** : `ModelProfileSpec` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional model profile specification. Defaults to a profile picked by the provider based on the model name. **`settings`** : [`ModelSettings`](/docs/ai/api/pydantic-ai/settings/#pydantic_ai.settings.ModelSettings) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional model settings. ##### supported\_native\_tools `@classmethod` ```python def supported_native_tools(cls) -> frozenset[type] ``` Return the set of builtin tool types this model can handle. ###### Returns [`frozenset`](https://docs.python.org/3/library/stdtypes.html#frozenset)\[[`type`](https://docs.python.org/3/glossary.html#term-type)\] ##### request `@async` ```python def request( messages: list[ModelMessage], model_settings: ModelSettings | None, model_request_parameters: ModelRequestParameters, ) -> ModelResponse ``` Make a request to the xAI model. ###### Returns [`ModelResponse`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.ModelResponse) ##### request\_stream `@async` ```python def request_stream( messages: list[ModelMessage], model_settings: ModelSettings | None, model_request_parameters: ModelRequestParameters, run_context: RunContext[Any] | None = None, ) -> AsyncIterator[StreamedResponse] ``` Make a streaming request to the xAI model. ###### Returns [`AsyncIterator`](https://docs.python.org/3/library/typing.html#typing.AsyncIterator)\[`StreamedResponse`\] ### XaiStreamedResponse **Bases:** `StreamedResponse` Implementation of `StreamedResponse` for xAI SDK. #### Attributes ##### system The model provider system name. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### provider\_url Get the provider base URL. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### model\_name Get the model name of the response. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### provider\_name The model provider. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### timestamp Get the timestamp of the response. **Type:** [`datetime`](https://docs.python.org/3/library/datetime.html#module-datetime) ### XAI\_EFFORT\_MAP Maps unified thinking values to xAI reasoning\_effort. xAI only supports 'low' and 'high'. **Type:** [`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[`ThinkingLevel`, [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['low', 'high'\]\] **Default:** `{True: 'high', 'minimal': 'low', 'low': 'low', 'medium': 'high', 'high': 'high', 'xhigh': 'high'}` ### XaiModelName Possible xAI model names. **Default:** `str | ChatModel` --- # [pydantic_ai.ag_ui](https://pydantic.dev/docs/ai/api/pydantic-ai/ag_ui/) # pydantic\_ai.ag\_ui Provides an AG-UI protocol adapter for the Pydantic AI agent. This package provides seamless integration between pydantic-ai agents and ag-ui for building interactive AI applications with streaming event-based communication. ### AGUIApp **Bases:** `Generic[AgentDepsT, OutputDataT]`, `Starlette` ASGI application for running Pydantic AI agents with AG-UI protocol support. #### Methods ##### \_\_init\_\_ `@deprecated` ```python def __init__( agent: AbstractAgent[AgentDepsT, OutputDataT], ag_ui_version: str = DEFAULT_AG_UI_VERSION, preserve_file_data: bool = False, output_type: OutputSpec[Any] | None = None, message_history: Sequence[ModelMessage] | None = None, deferred_tool_results: DeferredToolResults | None = None, conversation_id: str | None = None, model: Model | KnownModelName | str | None = None, deps: AgentDepsT = None, model_settings: ModelSettings | None = None, usage_limits: UsageLimits | None = None, usage: RunUsage | None = None, infer_name: bool = True, toolsets: Sequence[AbstractToolset[AgentDepsT]] | None = None, capabilities: Sequence[AbstractCapability[AgentDepsT]] | None = None, on_complete: OnCompleteFunc[Any] | None = None, debug: bool = False, routes: Sequence[BaseRoute] | None = None, middleware: Sequence[Middleware] | None = None, exception_handlers: Mapping[Any, ExceptionHandler] | None = None, on_startup: Sequence[Callable[[], Any]] | None = None, on_shutdown: Sequence[Callable[[], Any]] | None = None, lifespan: Lifespan[Self] | None = None, _deprecated_kwargs: Any = {}, ) -> None ``` An ASGI application that handles every request by running the agent and streaming the response. Note that the `deps` will be the same for each request, with the exception of the frontend state that's injected into the `state` field of a `deps` object that implements the [`StateHandler`](/docs/ai/api/ui/base/#pydantic_ai.ui.StateHandler) protocol. To provide different `deps` for each request (e.g. based on the authenticated user), use `AGUIAdapter.run_stream()` or `AGUIAdapter.dispatch_request()` instead. ###### Returns [`None`](https://docs.python.org/3/library/constants.html#None) ###### Parameters **`agent`** : `AbstractAgent`\[`AgentDepsT`, `OutputDataT`\] The agent to run. **`ag_ui_version`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) _Default:_ `DEFAULT_AG_UI_VERSION` AG-UI protocol version controlling thinking/reasoning event format. **`preserve_file_data`** : [`bool`](https://docs.python.org/3/library/functions.html#bool) _Default:_ `False` Whether to preserve agent-generated files and uploaded files in AG-UI message conversion. See [`AGUIAdapter.preserve_file_data`](/docs/ai/api/ui/ag_ui/#pydantic_ai.ui.ag_ui.AGUIAdapter.preserve_file_data). **`output_type`** : `OutputSpec`\[[`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Custom output type to use for this run, `output_type` may only be used if the agent has no output validators since output validators would expect an argument that matches the agent's output type. **`message_history`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`ModelMessage`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.ModelMessage)\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` History of the conversation so far. **`deferred_tool_results`** : [`DeferredToolResults`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.DeferredToolResults) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional results for deferred tool calls in the message history. **`conversation_id`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` ID of the conversation this run belongs to. Pass `'new'` to start a fresh conversation, ignoring any `conversation_id` already on `message_history`. If omitted, falls back to the most recent `conversation_id` on `message_history` or a freshly generated UUID7. **`model`** : `Model` | `KnownModelName` | [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional model to use for this run, required if `model` was not set when creating the agent. **`deps`** : `AgentDepsT` _Default:_ `None` Optional dependencies to use for this run. **`model_settings`** : [`ModelSettings`](/docs/ai/api/pydantic-ai/settings/#pydantic_ai.settings.ModelSettings) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional settings to use for this model's request. **`usage_limits`** : [`UsageLimits`](/docs/ai/api/pydantic-ai/usage/#pydantic_ai.usage.UsageLimits) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional limits on model request count or token usage. **`usage`** : [`RunUsage`](/docs/ai/api/pydantic-ai/usage/#pydantic_ai.usage.RunUsage) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional usage to start with, useful for resuming a conversation or agents used in tools. **`infer_name`** : [`bool`](https://docs.python.org/3/library/functions.html#bool) _Default:_ `True` Whether to try to infer the agent name from the call frame if it's not set. **`toolsets`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`AbstractToolset`](/docs/ai/api/pydantic-ai/toolsets/#pydantic_ai.toolsets.AbstractToolset)\[`AgentDepsT`\]\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional additional toolsets for this run. **`capabilities`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[`AbstractCapability`\[`AgentDepsT`\]\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional additional [capabilities](https://ai.pydantic.dev/capabilities/) for every run handled by this app, merged with the agent's configured capabilities. Use `capabilities=[NativeTool(...)]` to add provider-side native tools per app. **`on_complete`** : `OnCompleteFunc`\[[`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional callback function called when the agent run completes successfully. The callback receives the completed [`AgentRunResult`](/docs/ai/api/pydantic-ai/run/#pydantic_ai.run.AgentRunResult) and can access `all_messages()` and other result data. **`debug`** : [`bool`](https://docs.python.org/3/library/functions.html#bool) _Default:_ `False` Boolean indicating if debug tracebacks should be returned on errors. **`routes`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[`BaseRoute`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` A list of routes to serve incoming HTTP and WebSocket requests. **`middleware`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[`Middleware`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` A list of middleware to run for every request. A starlette application will always automatically include two middleware classes. `ServerErrorMiddleware` is added as the very outermost middleware, to handle any uncaught errors occurring anywhere in the entire stack. `ExceptionMiddleware` is added as the very innermost middleware, to deal with handled exception cases occurring in the routing or endpoints. **`exception_handlers`** : [`Mapping`](https://docs.python.org/3/library/typing.html#typing.Mapping)\[[`Any`](https://docs.python.org/3/library/typing.html#typing.Any), `ExceptionHandler`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` A mapping of either integer status codes, or exception class types onto callables which handle the exceptions. Exception handler callables should be of the form `handler(request, exc) -> response` and may be either standard functions, or async functions. **`on_startup`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`Callable`](https://docs.python.org/3/library/typing.html#typing.Callable)\[\[\], [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\]\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` A list of callables to run on application startup. Startup handler callables do not take any arguments, and may be either standard functions, or async functions. **`on_shutdown`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`Callable`](https://docs.python.org/3/library/typing.html#typing.Callable)\[\[\], [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\]\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` A list of callables to run on application shutdown. Shutdown handler callables do not take any arguments, and may be either standard functions, or async functions. **`lifespan`** : `Lifespan`\[[`Self`](https://docs.python.org/3/library/typing.html#typing.Self)\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` A lifespan context function, which can be used to perform startup and shutdown tasks. This is a newer style that replaces the `on_startup` and `on_shutdown` handlers. Use one or the other, not both. ### StateHandler **Bases:** [`Protocol`](https://docs.python.org/3/library/typing.html#typing.Protocol) Protocol for state handlers in agent runs. Requires the class to be a dataclass with a `state` field. #### Attributes ##### state Get the current state of the agent run. **Type:** [`Any`](https://docs.python.org/3/library/typing.html#typing.Any) ### StateDeps **Bases:** `Generic[StateT]` Dependency type that holds state. This class is used to manage the state of an agent run. It allows setting the state of the agent run with a specific type of state model, which must be a subclass of `BaseModel`. The state is set using the `state` setter by the `Adapter` when the run starts. Implements the `StateHandler` protocol. ### handle\_ag\_ui\_request `@async` ```python def handle_ag_ui_request( agent: AbstractAgent[AgentDepsT, Any], request: Request, ag_ui_version: str = DEFAULT_AG_UI_VERSION, preserve_file_data: bool = False, output_type: OutputSpec[Any] | None = None, message_history: Sequence[ModelMessage] | None = None, deferred_tool_results: DeferredToolResults | None = None, conversation_id: str | None = None, model: Model | KnownModelName | str | None = None, deps: AgentDepsT = None, model_settings: ModelSettings | None = None, usage_limits: UsageLimits | None = None, usage: RunUsage | None = None, metadata: AgentMetadata[AgentDepsT] | None = None, infer_name: bool = True, toolsets: Sequence[AbstractToolset[AgentDepsT]] | None = None, on_complete: OnCompleteFunc[BaseEvent] | None = None, manage_system_prompt: Literal['server', 'client'] = 'server', allowed_file_url_schemes: frozenset[str] = frozenset({'http', 'https'}), ) -> Response ``` Handle an AG-UI request by running the agent and returning a streaming response. #### Returns `Response` -- A streaming Starlette response with AG-UI protocol events. #### Parameters **`agent`** : `AbstractAgent`\[`AgentDepsT`, [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] The agent to run. **`request`** : `Request` The Starlette request (e.g. from FastAPI) containing the AG-UI run input. **`ag_ui_version`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) _Default:_ `DEFAULT_AG_UI_VERSION` AG-UI protocol version controlling thinking/reasoning event format. **`preserve_file_data`** : [`bool`](https://docs.python.org/3/library/functions.html#bool) _Default:_ `False` Whether to preserve agent-generated files and uploaded files in AG-UI message conversion. See [`AGUIAdapter.preserve_file_data`](/docs/ai/api/ui/ag_ui/#pydantic_ai.ui.ag_ui.AGUIAdapter.preserve_file_data). **`output_type`** : `OutputSpec`\[[`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Custom output type to use for this run, `output_type` may only be used if the agent has no output validators since output validators would expect an argument that matches the agent's output type. **`message_history`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`ModelMessage`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.ModelMessage)\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` History of the conversation so far. **`deferred_tool_results`** : [`DeferredToolResults`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.DeferredToolResults) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional results for deferred tool calls in the message history. **`conversation_id`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` ID of the conversation this run belongs to. Pass `'new'` to start a fresh conversation, ignoring any `conversation_id` already on `message_history`. If omitted, falls back to the most recent `conversation_id` on `message_history` or a freshly generated UUID7. **`model`** : `Model` | `KnownModelName` | [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional model to use for this run, required if `model` was not set when creating the agent. **`deps`** : `AgentDepsT` _Default:_ `None` Optional dependencies to use for this run. **`model_settings`** : [`ModelSettings`](/docs/ai/api/pydantic-ai/settings/#pydantic_ai.settings.ModelSettings) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional settings to use for this model's request. **`usage_limits`** : [`UsageLimits`](/docs/ai/api/pydantic-ai/usage/#pydantic_ai.usage.UsageLimits) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional limits on model request count or token usage. **`usage`** : [`RunUsage`](/docs/ai/api/pydantic-ai/usage/#pydantic_ai.usage.RunUsage) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional usage to start with, useful for resuming a conversation or agents used in tools. **`metadata`** : `AgentMetadata`\[`AgentDepsT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional metadata to attach to this run. Accepts a dictionary or a callable taking [`RunContext`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.RunContext); merged with the agent's configured metadata. **`infer_name`** : [`bool`](https://docs.python.org/3/library/functions.html#bool) _Default:_ `True` Whether to try to infer the agent name from the call frame if it's not set. **`toolsets`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`AbstractToolset`](/docs/ai/api/pydantic-ai/toolsets/#pydantic_ai.toolsets.AbstractToolset)\[`AgentDepsT`\]\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional additional toolsets for this run. **`on_complete`** : `OnCompleteFunc`\[`BaseEvent`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional callback function called when the agent run completes successfully. The callback receives the completed [`AgentRunResult`](/docs/ai/api/pydantic-ai/run/#pydantic_ai.run.AgentRunResult) and can access `all_messages()` and other result data. **`manage_system_prompt`** : [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['server', 'client'\] _Default:_ `'server'` Who owns the system prompt. See [`UIAdapter.manage_system_prompt`](/docs/ai/api/ui/base/#pydantic_ai.ui.UIAdapter.manage_system_prompt). **`allowed_file_url_schemes`** : [`frozenset`](https://docs.python.org/3/library/stdtypes.html#frozenset)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str)\] _Default:_ `frozenset({'http', 'https'})` URL schemes allowed for file URL parts from the client. See [`UIAdapter.allowed_file_url_schemes`](/docs/ai/api/ui/base/#pydantic_ai.ui.UIAdapter.allowed_file_url_schemes). ### run\_ag\_ui ```python def run_ag_ui( agent: AbstractAgent[AgentDepsT, Any], run_input: RunAgentInput, accept: str = SSE_CONTENT_TYPE, ag_ui_version: str = DEFAULT_AG_UI_VERSION, preserve_file_data: bool = False, output_type: OutputSpec[Any] | None = None, message_history: Sequence[ModelMessage] | None = None, deferred_tool_results: DeferredToolResults | None = None, conversation_id: str | None = None, model: Model | KnownModelName | str | None = None, deps: AgentDepsT = None, model_settings: ModelSettings | None = None, usage_limits: UsageLimits | None = None, usage: RunUsage | None = None, metadata: AgentMetadata[AgentDepsT] | None = None, infer_name: bool = True, toolsets: Sequence[AbstractToolset[AgentDepsT]] | None = None, on_complete: OnCompleteFunc[BaseEvent] | None = None, manage_system_prompt: Literal['server', 'client'] = 'server', allowed_file_url_schemes: frozenset[str] = frozenset({'http', 'https'}), ) -> AsyncIterator[str] ``` Run the agent with the AG-UI run input and stream AG-UI protocol events. #### Returns [`AsyncIterator`](https://docs.python.org/3/library/typing.html#typing.AsyncIterator)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str)\] #### Parameters **`agent`** : `AbstractAgent`\[`AgentDepsT`, [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] The agent to run. **`run_input`** : `RunAgentInput` The AG-UI run input containing thread\_id, run\_id, messages, etc. **`accept`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) _Default:_ `SSE_CONTENT_TYPE` The accept header value for the run. **`ag_ui_version`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) _Default:_ `DEFAULT_AG_UI_VERSION` AG-UI protocol version controlling thinking/reasoning event format. **`preserve_file_data`** : [`bool`](https://docs.python.org/3/library/functions.html#bool) _Default:_ `False` Whether to preserve agent-generated files and uploaded files in AG-UI message conversion. See [`AGUIAdapter.preserve_file_data`](/docs/ai/api/ui/ag_ui/#pydantic_ai.ui.ag_ui.AGUIAdapter.preserve_file_data). **`output_type`** : `OutputSpec`\[[`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Custom output type to use for this run, `output_type` may only be used if the agent has no output validators since output validators would expect an argument that matches the agent's output type. **`message_history`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`ModelMessage`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.ModelMessage)\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` History of the conversation so far. **`deferred_tool_results`** : [`DeferredToolResults`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.DeferredToolResults) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional results for deferred tool calls in the message history. **`conversation_id`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` ID of the conversation this run belongs to. Pass `'new'` to start a fresh conversation, ignoring any `conversation_id` already on `message_history`. If omitted, falls back to the most recent `conversation_id` on `message_history` or a freshly generated UUID7. **`model`** : `Model` | `KnownModelName` | [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional model to use for this run, required if `model` was not set when creating the agent. **`deps`** : `AgentDepsT` _Default:_ `None` Optional dependencies to use for this run. **`model_settings`** : [`ModelSettings`](/docs/ai/api/pydantic-ai/settings/#pydantic_ai.settings.ModelSettings) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional settings to use for this model's request. **`usage_limits`** : [`UsageLimits`](/docs/ai/api/pydantic-ai/usage/#pydantic_ai.usage.UsageLimits) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional limits on model request count or token usage. **`usage`** : [`RunUsage`](/docs/ai/api/pydantic-ai/usage/#pydantic_ai.usage.RunUsage) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional usage to start with, useful for resuming a conversation or agents used in tools. **`metadata`** : `AgentMetadata`\[`AgentDepsT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional metadata to attach to this run. Accepts a dictionary or a callable taking [`RunContext`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.RunContext); merged with the agent's configured metadata. **`infer_name`** : [`bool`](https://docs.python.org/3/library/functions.html#bool) _Default:_ `True` Whether to try to infer the agent name from the call frame if it's not set. **`toolsets`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`AbstractToolset`](/docs/ai/api/pydantic-ai/toolsets/#pydantic_ai.toolsets.AbstractToolset)\[`AgentDepsT`\]\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional additional toolsets for this run. **`on_complete`** : `OnCompleteFunc`\[`BaseEvent`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional callback function called when the agent run completes successfully. The callback receives the completed [`AgentRunResult`](/docs/ai/api/pydantic-ai/run/#pydantic_ai.run.AgentRunResult) and can access `all_messages()` and other result data. **`manage_system_prompt`** : [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['server', 'client'\] _Default:_ `'server'` Who owns the system prompt. See [`UIAdapter.manage_system_prompt`](/docs/ai/api/ui/base/#pydantic_ai.ui.UIAdapter.manage_system_prompt). **`allowed_file_url_schemes`** : [`frozenset`](https://docs.python.org/3/library/stdtypes.html#frozenset)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str)\] _Default:_ `frozenset({'http', 'https'})` URL schemes allowed for file URL parts from the client. See [`UIAdapter.allowed_file_url_schemes`](/docs/ai/api/ui/base/#pydantic_ai.ui.UIAdapter.allowed_file_url_schemes). ### SSE\_CONTENT\_TYPE Content type header value for Server-Sent Events (SSE). **Default:** `'text/event-stream'` ### OnCompleteFunc Callback function type that receives the `AgentRunResult` of the completed run. Can be sync, async, or an async generator of protocol-specific events. **Type:** [`TypeAlias`](https://docs.python.org/3/library/typing.html#typing.TypeAlias) **Default:** `Callable[[AgentRunResult[Any]], None] | Callable[[AgentRunResult[Any]], Awaitable[None]] | Callable[[AgentRunResult[Any]], AsyncIterator[EventT]]` --- # [pydantic_ai.agent](https://pydantic.dev/docs/ai/api/pydantic-ai/agent/) # pydantic\_ai.agent ### Agent **Bases:** `AbstractAgent[AgentDepsT, OutputDataT]` Class for defining "agents" - a way to have a specific type of "conversation" with an LLM. Agents are generic in the dependency type they take [`AgentDepsT`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.AgentDepsT) and the output type they return, [`OutputDataT`](/docs/ai/api/pydantic-ai/output/#pydantic_ai.output.OutputDataT). By default, if neither generic parameter is customised, agents have type `Agent[None, str]`. Minimal usage example: ```python from pydantic_ai import Agent agent = Agent('openai:gpt-5.2') result = agent.run_sync('What is the capital of France?') print(result.output) #> The capital of France is Paris. ``` #### Attributes ##### end\_strategy The strategy for handling multiple tool calls when a final result is found. - `'early'` (default): Output tools are executed first. Once a valid final result is found, remaining function and output tool calls are skipped - `'graceful'`: Output tools are executed first. Once a valid final result is found, remaining output tool calls are skipped, but function tools are still executed - `'exhaustive'`: Output tools are executed first, then all function tools are executed. The first valid output tool result becomes the final output **Type:** [`EndStrategy`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.EndStrategy) **Default:** `end_strategy` ##### model\_settings Optional model request settings to use for this agent's runs, by default. Can be a static `ModelSettings` dict or a callable that takes a [`RunContext`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.RunContext) and returns `ModelSettings`. Callables are called before each model request, allowing dynamic per-step settings. Note, if `model_settings` is also provided at run time, those settings will be merged on top of the agent-level settings, with the run-level argument taking priority. **Type:** `AgentModelSettings`\[`AgentDepsT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `model_settings` ##### instrument Instrumentation settings applied to this agent. **Type:** [`InstrumentationSettings`](/docs/ai/api/models/instrumented/#pydantic_ai.models.instrumented.InstrumentationSettings) | [`bool`](https://docs.python.org/3/library/functions.html#bool) | [`None`](https://docs.python.org/3/library/constants.html#None) ##### model The default model configured for this agent. **Type:** [`models.Model`](/docs/ai/api/models/base/#pydantic_ai.models.Model) | [`models.KnownModelName`](/docs/ai/api/models/base/#pydantic_ai.models.KnownModelName) | [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) ##### name The name of the agent, used for logging. If `None`, we try to infer the agent name from the call frame when the agent is first run. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) ##### description A human-readable description of the agent. If the description is a TemplateStr, returns the raw template source. The rendered description is available at runtime via OTel span attributes. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) ##### deps\_type The type of dependencies used by the agent. **Type:** [`type`](https://docs.python.org/3/glossary.html#term-type) ##### output\_type The type of data output by agent runs, used to validate the data returned by the model, defaults to `str`. **Type:** `OutputSpec`\[`OutputDataT`\] ##### event\_stream\_handler Optional handler for events from the model's streaming response and the agent's execution of tools. **Type:** `EventStreamHandler`\[`AgentDepsT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) ##### root\_capability The root capability of the agent, containing all registered capabilities. **Type:** `CombinedCapability`\[`AgentDepsT`\] ##### toolsets All toolsets registered on the agent, including a function toolset holding tools that were registered on the agent directly. Output tools are not included. **Type:** [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`AbstractToolset`](/docs/ai/api/pydantic-ai/toolsets/#pydantic_ai.toolsets.AbstractToolset)\[`AgentDepsT`\]\] #### Methods ##### \_\_init\_\_ ```python def __init__( model: models.Model | models.KnownModelName | str | None = None, output_type: OutputSpec[OutputDataT] = str, instructions: AgentInstructions[AgentDepsT] = None, system_prompt: str | Sequence[str] = (), deps_type: type[AgentDepsT] = NoneType, name: str | None = None, description: TemplateStr[AgentDepsT] | str | None = None, model_settings: AgentModelSettings[AgentDepsT] | None = None, retries: int | AgentRetries | None = None, validation_context: Any | Callable[[RunContext[AgentDepsT]], Any] = None, tools: Sequence[Tool[AgentDepsT] | ToolFuncEither[AgentDepsT, ...]] = (), toolsets: Sequence[AgentToolset[AgentDepsT]] | None = None, defer_model_check: bool = False, end_strategy: EndStrategy = 'early', metadata: AgentMetadata[AgentDepsT] | None = None, tool_timeout: float | None = None, max_concurrency: _concurrency.AnyConcurrencyLimit = None, capabilities: Sequence[AgentCapability[AgentDepsT]] | None = None, ) -> None def __init__( model: models.Model | models.KnownModelName | str | None = None, output_type: OutputSpec[OutputDataT] = str, instructions: AgentInstructions[AgentDepsT] = None, system_prompt: str | Sequence[str] = (), deps_type: type[AgentDepsT] = NoneType, name: str | None = None, description: TemplateStr[AgentDepsT] | str | None = None, model_settings: AgentModelSettings[AgentDepsT] | None = None, retries: int | AgentRetries | None = None, validation_context: Any | Callable[[RunContext[AgentDepsT]], Any] = None, tools: Sequence[Tool[AgentDepsT] | ToolFuncEither[AgentDepsT, ...]] = (), mcp_servers: Sequence[MCPServer] = (), defer_model_check: bool = False, end_strategy: EndStrategy = 'early', metadata: AgentMetadata[AgentDepsT] | None = None, tool_timeout: float | None = None, max_concurrency: _concurrency.AnyConcurrencyLimit = None, capabilities: Sequence[AgentCapability[AgentDepsT]] | None = None, ) -> None ``` Create an agent. ###### Parameters **`model`** : [`models.Model`](/docs/ai/api/models/base/#pydantic_ai.models.Model) | [`models.KnownModelName`](/docs/ai/api/models/base/#pydantic_ai.models.KnownModelName) | [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` The default model to use for this agent, if not provided, you must provide the model when calling it. We allow `str` here since the actual list of allowed models changes frequently. **`output_type`** : `OutputSpec`\[`OutputDataT`\] _Default:_ `str` The type of the output data, used to validate the data returned by the model, defaults to `str`. **`instructions`** : `AgentInstructions`\[`AgentDepsT`\] _Default:_ `None` Instructions to use for this agent, you can also register instructions via a function with [`instructions`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.Agent.instructions) or pass additional, temporary, instructions when executing a run. **`system_prompt`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str)\] _Default:_ `()` Static system prompts to use for this agent, you can also register system prompts via a function with [`system_prompt`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.Agent.system_prompt). **`deps_type`** : [`type`](https://docs.python.org/3/glossary.html#term-type)\[`AgentDepsT`\] _Default:_ `NoneType` The type used for dependency injection, this parameter exists solely to allow you to fully parameterize the agent, and therefore get the best out of static type checking. If you're not using deps, but want type checking to pass, you can set `deps=None` to satisfy Pyright or add a type hint `: Agent[None, ]`. **`name`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` The name of the agent, used for logging. If `None`, we try to infer the agent name from the call frame when the agent is first run. **`description`** : `TemplateStr`\[`AgentDepsT`\] | [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` A human-readable description of the agent, attached to the agent run span as `gen_ai.agent.description` when instrumentation is enabled. **`model_settings`** : `AgentModelSettings`\[`AgentDepsT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional model request settings to use for this agent's runs, by default. Can be a static `ModelSettings` dict or a callable that takes a [`RunContext`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.RunContext) and returns `ModelSettings`. Callables are called before each model request, allowing dynamic per-step settings. **`retries`** : [`int`](https://docs.python.org/3/library/functions.html#int) | [`AgentRetries`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.AgentRetries) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Per-category retry budgets for tools and output validation. Pass an `int` to set the same budget for both, or an [`AgentRetries`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.AgentRetries) dict to set them individually (e.g. `retries={'tools': 3, 'output': 1}`). Defaults to 1 for both. On the text path, `output` is a global budget shared across all output-validation retries in a run; on the tool path it is the default per-tool `max_retries` for each output tool, overridable via [`ToolOutput(max_retries=...)`](/docs/ai/api/pydantic-ai/output/#pydantic_ai.output.ToolOutput.max_retries). The `output` budget can be overridden per run via `agent.run(retries=...)` (and friends). For model request retries, see the [HTTP Request Retries](/docs/ai/advanced-features/retries) documentation. **`validation_context`** : [`Any`](https://docs.python.org/3/library/typing.html#typing.Any) | [`Callable`](https://docs.python.org/3/library/typing.html#typing.Callable)\[\[[`RunContext`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.RunContext)\[`AgentDepsT`\]\], [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] _Default:_ `None` Pydantic [validation context](https://docs.pydantic.dev/latest/concepts/validators/#validation-context) used to validate tool arguments and outputs. **`tools`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`Tool`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.Tool)\[`AgentDepsT`\] | `ToolFuncEither`\[`AgentDepsT`, ...\]\] _Default:_ `()` Tools to register with the agent, you can also register tools via the decorators [`@agent.tool`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.Agent.tool) and [`@agent.tool_plain`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.Agent.tool_plain). **`toolsets`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[`AgentToolset`\[`AgentDepsT`\]\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Toolsets to register with the agent, including MCP servers and functions which take a run context and return a toolset. See [`ToolsetFunc`](/docs/ai/api/pydantic-ai/toolsets/#pydantic_ai.toolsets.ToolsetFunc) for more information. **`defer_model_check`** : [`bool`](https://docs.python.org/3/library/functions.html#bool) _Default:_ `False` by default, if you provide a [named](/docs/ai/api/models/base/#pydantic_ai.models.KnownModelName) model, it's evaluated to create a [`Model`](/docs/ai/api/models/base/#pydantic_ai.models.Model) instance immediately, which checks for the necessary environment variables. Set this to `false` to defer the evaluation until the first run. Useful if you want to [override the model](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.Agent.override) for testing. **`end_strategy`** : [`EndStrategy`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.EndStrategy) _Default:_ `'early'` Strategy for handling tool calls that are requested alongside a final result. See [`EndStrategy`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.EndStrategy) for more information. **`metadata`** : `AgentMetadata`\[`AgentDepsT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional metadata to store with each run. Provide a dictionary of primitives, or a callable returning one computed from the [`RunContext`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.RunContext) on each run. Metadata is resolved when a run starts and recomputed after a successful run finishes so it can reflect the final state. Resolved metadata can be read after the run completes via [`AgentRun.metadata`](/docs/ai/api/pydantic-ai/run/#pydantic_ai.run.AgentRun), [`AgentRunResult.metadata`](/docs/ai/api/pydantic-ai/run/#pydantic_ai.run.AgentRunResult), and [`StreamedRunResult.metadata`](/docs/ai/api/pydantic-ai/result/#pydantic_ai.result.StreamedRunResult), and is attached to the agent run span when instrumentation is enabled. **`tool_timeout`** : [`float`](https://docs.python.org/3/library/functions.html#float) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Default timeout in seconds for tool execution. If a tool takes longer than this, the tool is considered to have failed and a retry prompt is returned to the model (counting towards the retry limit). Individual tools can override this with their own timeout. Defaults to None (no timeout). **`max_concurrency`** : `_concurrency.AnyConcurrencyLimit` _Default:_ `None` Optional limit on concurrent agent runs. Can be an integer for simple limiting, a [`ConcurrencyLimit`](/docs/ai/api/pydantic-ai/concurrency/#pydantic_ai.ConcurrencyLimit) for advanced configuration with backpressure, a [`ConcurrencyLimiter`](/docs/ai/api/pydantic-ai/concurrency/#pydantic_ai.ConcurrencyLimiter) for sharing limits across multiple agents, or None (default) for no limiting. When the limit is reached, additional calls to `run()` or `iter()` will wait until a slot becomes available. **`capabilities`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`AgentCapability`](/docs/ai/api/pydantic-ai/capabilities/#pydantic_ai.capabilities.AgentCapability)\[`AgentDepsT`\]\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional list of [capabilities](https://ai.pydantic.dev/capabilities/) to configure the agent with, including functions which take a run context and return a capability. See [`CapabilityFunc`](/docs/ai/api/pydantic-ai/capabilities/#pydantic_ai.capabilities.CapabilityFunc) for more information. Custom capabilities can be created by subclassing [`AbstractCapability`](/docs/ai/api/pydantic-ai/capabilities/#pydantic_ai.capabilities.AbstractCapability). ##### from\_spec `@classmethod` ```python def from_spec( cls, spec: dict[str, Any] | AgentSpec, custom_capability_types: Sequence[type[AbstractCapability[Any]]] = (), model: models.Model | models.KnownModelName | str | None = None, output_type: OutputSpec[Any] = str, instructions: AgentInstructions[Any] = None, system_prompt: str | Sequence[str] = (), name: str | None = None, description: TemplateStr[Any] | str | None = None, model_settings: ModelSettings | None = None, retries: int | AgentRetries | None = None, validation_context: Any = None, tools: Sequence[Tool[Any] | ToolFuncEither[Any, ...]] = (), toolsets: Sequence[AgentToolset[Any]] | None = None, defer_model_check: bool = False, end_strategy: EndStrategy | None = None, metadata: AgentMetadata[Any] | None = None, tool_timeout: float | None = None, max_concurrency: _concurrency.AnyConcurrencyLimit = None, capabilities: Sequence[AgentCapability[Any]] | None = None, ) -> Agent[None, str] def from_spec( cls, spec: dict[str, Any] | AgentSpec, deps_type: type[T], custom_capability_types: Sequence[type[AbstractCapability[Any]]] = (), model: models.Model | models.KnownModelName | str | None = None, output_type: OutputSpec[Any] = str, instructions: AgentInstructions[Any] = None, system_prompt: str | Sequence[str] = (), name: str | None = None, description: TemplateStr[Any] | str | None = None, model_settings: ModelSettings | None = None, retries: int | AgentRetries | None = None, validation_context: Any = None, tools: Sequence[Tool[Any] | ToolFuncEither[Any, ...]] = (), toolsets: Sequence[AgentToolset[Any]] | None = None, defer_model_check: bool = False, end_strategy: EndStrategy | None = None, metadata: AgentMetadata[Any] | None = None, tool_timeout: float | None = None, max_concurrency: _concurrency.AnyConcurrencyLimit = None, capabilities: Sequence[AgentCapability[Any]] | None = None, ) -> Agent[T, str] ``` Construct an Agent from a spec dict or `AgentSpec`. This allows defining agents declaratively in YAML/JSON/dict form. Keyword arguments supplement the spec: scalar spec fields (like `name`, `retries`) are used as defaults that explicit arguments override, while `capabilities` from both sources are merged. ###### Returns [`Agent`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.Agent)\[[`Any`](https://docs.python.org/3/library/typing.html#typing.Any), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] -- A new Agent instance. ###### Parameters **`spec`** : [`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] | `AgentSpec` The agent specification, either a dict or an `AgentSpec` instance. **`deps_type`** : [`type`](https://docs.python.org/3/glossary.html#term-type)\[[`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] _Default:_ `type(None)` The type of the dependencies for the agent. When provided, template strings in capabilities (e.g. `"Hello {{name}}"`) are compiled and validated against this type. **`custom_capability_types`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`type`](https://docs.python.org/3/glossary.html#term-type)\[`AbstractCapability`\[[`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\]\]\] _Default:_ `()` Additional capability classes to make available beyond the built-in defaults. **`model`** : [`models.Model`](/docs/ai/api/models/base/#pydantic_ai.models.Model) | [`models.KnownModelName`](/docs/ai/api/models/base/#pydantic_ai.models.KnownModelName) | [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Override the model from the spec. **`output_type`** : `OutputSpec`\[[`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] _Default:_ `str` The type of the output data, defaults to `str`. **`instructions`** : `AgentInstructions`\[[`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] _Default:_ `None` Instructions for the agent. **`system_prompt`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str)\] _Default:_ `()` Static system prompts. **`name`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` The agent name, overrides spec `name` if provided. **`description`** : `TemplateStr`\[[`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] | [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` The agent description, overrides spec `description` if provided. **`model_settings`** : [`ModelSettings`](/docs/ai/api/pydantic-ai/settings/#pydantic_ai.settings.ModelSettings) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Model request settings. **`retries`** : [`int`](https://docs.python.org/3/library/functions.html#int) | [`AgentRetries`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.AgentRetries) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Retry budgets for tools and output validation. Pass an `int` to set the same budget for both, or an [`AgentRetries`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.AgentRetries) dict to set them individually. Overrides spec `retries` if provided. **`validation_context`** : [`Any`](https://docs.python.org/3/library/typing.html#typing.Any) _Default:_ `None` Pydantic validation context for tool arguments and outputs. **`tools`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`Tool`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.Tool)\[[`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] | `ToolFuncEither`\[[`Any`](https://docs.python.org/3/library/typing.html#typing.Any), ...\]\] _Default:_ `()` Tools to register with the agent. **`toolsets`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[`AgentToolset`\[[`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\]\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Toolsets to register with the agent. **`defer_model_check`** : [`bool`](https://docs.python.org/3/library/functions.html#bool) _Default:_ `False` Defer model evaluation until first run. **`end_strategy`** : [`EndStrategy`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.EndStrategy) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Strategy for tool calls alongside a final result, overrides spec `end_strategy` if provided. **`metadata`** : `AgentMetadata`\[[`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Metadata to store with each run, overrides spec `metadata` if provided. **`tool_timeout`** : [`float`](https://docs.python.org/3/library/functions.html#float) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Default timeout for tool execution, overrides spec `tool_timeout` if provided. **`max_concurrency`** : `_concurrency.AnyConcurrencyLimit` _Default:_ `None` Limit on concurrent agent runs. **`capabilities`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`AgentCapability`](/docs/ai/api/pydantic-ai/capabilities/#pydantic_ai.capabilities.AgentCapability)\[[`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\]\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Additional capabilities merged with those from the spec. ##### from\_file `@classmethod` ```python def from_file( cls, path: Path | str, fmt: Literal['yaml', 'json'] | None = None, custom_capability_types: Sequence[type[AbstractCapability[Any]]] = (), model: models.Model | models.KnownModelName | str | None = None, output_type: OutputSpec[Any] = str, instructions: AgentInstructions[Any] = None, system_prompt: str | Sequence[str] = (), name: str | None = None, description: TemplateStr[Any] | str | None = None, model_settings: ModelSettings | None = None, retries: int | AgentRetries | None = None, validation_context: Any = None, tools: Sequence[Tool[Any] | ToolFuncEither[Any, ...]] = (), toolsets: Sequence[AgentToolset[Any]] | None = None, defer_model_check: bool = False, end_strategy: EndStrategy | None = None, metadata: AgentMetadata[Any] | None = None, tool_timeout: float | None = None, max_concurrency: _concurrency.AnyConcurrencyLimit = None, capabilities: Sequence[AgentCapability[Any]] | None = None, ) -> Agent[None, str] def from_file( cls, path: Path | str, fmt: Literal['yaml', 'json'] | None = None, deps_type: type[T], custom_capability_types: Sequence[type[AbstractCapability[Any]]] = (), model: models.Model | models.KnownModelName | str | None = None, output_type: OutputSpec[Any] = str, instructions: AgentInstructions[Any] = None, system_prompt: str | Sequence[str] = (), name: str | None = None, description: TemplateStr[Any] | str | None = None, model_settings: ModelSettings | None = None, retries: int | AgentRetries | None = None, validation_context: Any = None, tools: Sequence[Tool[Any] | ToolFuncEither[Any, ...]] = (), toolsets: Sequence[AgentToolset[Any]] | None = None, defer_model_check: bool = False, end_strategy: EndStrategy | None = None, metadata: AgentMetadata[Any] | None = None, tool_timeout: float | None = None, max_concurrency: _concurrency.AnyConcurrencyLimit = None, capabilities: Sequence[AgentCapability[Any]] | None = None, ) -> Agent[T, str] ``` Construct an Agent from a YAML or JSON spec file. This is a convenience method equivalent to `Agent.from_spec(AgentSpec.from_file(path), ...)`. The file format is inferred from the extension (`.yaml`/`.yml` or `.json`) unless overridden with the `fmt` argument. All other arguments are forwarded to [`from_spec`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.Agent.from_spec). ###### Returns [`Agent`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.Agent)\[[`Any`](https://docs.python.org/3/library/typing.html#typing.Any), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] ##### instrument\_all `@staticmethod` ```python def instrument_all(instrument: InstrumentationSettings | bool = True) -> None ``` Set the instrumentation options for all agents that don't explicitly add an `Instrumentation` capability. ###### Returns [`None`](https://docs.python.org/3/library/constants.html#None) ##### render\_description ```python def render_description(deps: AgentDepsT = None) -> str | None ``` Return the agent description, rendering any TemplateStr with the given deps. ###### Returns [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) ##### iter `@async` ```python def iter( user_prompt: str | Sequence[_messages.UserContent] | None = None, output_type: None = None, message_history: Sequence[_messages.ModelMessage] | None = None, deferred_tool_results: DeferredToolResults | None = None, conversation_id: str | None = None, model: models.Model | models.KnownModelName | str | None = None, instructions: AgentInstructions[AgentDepsT] = None, deps: AgentDepsT = None, model_settings: AgentModelSettings[AgentDepsT] | None = None, usage_limits: _usage.UsageLimits | None = None, usage: _usage.RunUsage | None = None, metadata: AgentMetadata[AgentDepsT] | None = None, retries: int | AgentRetries | None = None, infer_name: bool = True, toolsets: Sequence[AbstractToolset[AgentDepsT]] | None = None, capabilities: Sequence[AgentCapability[AgentDepsT]] | None = None, spec: dict[str, Any] | AgentSpec | None = None, ) -> AbstractAsyncContextManager[AgentRun[AgentDepsT, OutputDataT]] def iter( user_prompt: str | Sequence[_messages.UserContent] | None = None, output_type: OutputSpec[RunOutputDataT], message_history: Sequence[_messages.ModelMessage] | None = None, deferred_tool_results: DeferredToolResults | None = None, conversation_id: str | None = None, model: models.Model | models.KnownModelName | str | None = None, instructions: AgentInstructions[AgentDepsT] = None, deps: AgentDepsT = None, model_settings: AgentModelSettings[AgentDepsT] | None = None, usage_limits: _usage.UsageLimits | None = None, usage: _usage.RunUsage | None = None, metadata: AgentMetadata[AgentDepsT] | None = None, retries: int | AgentRetries | None = None, infer_name: bool = True, toolsets: Sequence[AbstractToolset[AgentDepsT]] | None = None, capabilities: Sequence[AgentCapability[AgentDepsT]] | None = None, spec: dict[str, Any] | AgentSpec | None = None, ) -> AbstractAsyncContextManager[AgentRun[AgentDepsT, RunOutputDataT]] ``` A contextmanager which can be used to iterate over the agent graph's nodes as they are executed. This method builds an internal agent graph (using system prompts, tools and output schemas) and then returns an `AgentRun` object. The `AgentRun` can be used to async-iterate over the nodes of the graph as they are executed. This is the API to use if you want to consume the outputs coming from each LLM model response, or the stream of events coming from the execution of tools. The `AgentRun` also provides methods to access the full message history, new messages, and usage statistics, and the final result of the run once it has completed. For more details, see the documentation of `AgentRun`. Example: ```python from pydantic_ai import Agent agent = Agent('openai:gpt-5.2') async def main(): nodes = [] async with agent.iter('What is the capital of France?') as agent_run: async for node in agent_run: nodes.append(node) print(nodes) ''' [ UserPromptNode( user_prompt='What is the capital of France?', instructions_functions=[], system_prompts=(), system_prompt_functions=[], system_prompt_dynamic_functions={}, ), ModelRequestNode( request=ModelRequest( parts=[ UserPromptPart( content='What is the capital of France?', timestamp=datetime.datetime(...), ) ], timestamp=datetime.datetime(...), run_id='...', conversation_id='...', ) ), CallToolsNode( model_response=ModelResponse( parts=[TextPart(content='The capital of France is Paris.')], usage=RequestUsage(input_tokens=56, output_tokens=7), model_name='gpt-5.2', timestamp=datetime.datetime(...), run_id='...', conversation_id='...', ) ), End(data=FinalResult(output='The capital of France is Paris.')), ] ''' print(agent_run.result.output) #> The capital of France is Paris. ``` ###### Returns [`AsyncIterator`](https://docs.python.org/3/library/typing.html#typing.AsyncIterator)\[[`AgentRun`](/docs/ai/api/pydantic-ai/run/#pydantic_ai.run.AgentRun)\[`AgentDepsT`, [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\]\] -- The result of the run. ###### Parameters **`user_prompt`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[`_messages.UserContent`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` User input to start/continue the conversation. **`output_type`** : `OutputSpec`\[[`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Custom output type to use for this run, `output_type` may only be used if the agent has no output validators since output validators would expect an argument that matches the agent's output type. **`message_history`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[`_messages.ModelMessage`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` History of the conversation so far. **`deferred_tool_results`** : [`DeferredToolResults`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.DeferredToolResults) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional results for deferred tool calls in the message history. **`conversation_id`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` ID of the conversation this run belongs to. Pass `'new'` to start a fresh conversation, ignoring any `conversation_id` already on `message_history`. If omitted, falls back to the most recent `conversation_id` on `message_history` or a freshly generated UUID7. **`model`** : [`models.Model`](/docs/ai/api/models/base/#pydantic_ai.models.Model) | [`models.KnownModelName`](/docs/ai/api/models/base/#pydantic_ai.models.KnownModelName) | [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional model to use for this run, required if `model` was not set when creating the agent. **`instructions`** : `AgentInstructions`\[`AgentDepsT`\] _Default:_ `None` Optional additional instructions to use for this run. **`deps`** : `AgentDepsT` _Default:_ `None` Optional dependencies to use for this run. **`model_settings`** : `AgentModelSettings`\[`AgentDepsT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional settings to use for this model's request, or a callable that receives [`RunContext`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.RunContext) and returns settings. Callables are called before each model request, allowing dynamic per-step settings. **`usage_limits`** : `_usage.UsageLimits` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional limits on model request count or token usage. **`usage`** : `_usage.RunUsage` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional usage to start with, useful for resuming a conversation or agents used in tools. **`metadata`** : `AgentMetadata`\[`AgentDepsT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional metadata to attach to this run. Accepts a dictionary or a callable taking [`RunContext`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.RunContext); merged with the agent's configured metadata. **`retries`** : [`int`](https://docs.python.org/3/library/functions.html#int) | [`AgentRetries`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.AgentRetries) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Override the agent-level retry budgets for this run. Pass an `int` to override the output-validation budget (`AgentRetries(output=...)` equivalent), or an [`AgentRetries`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.AgentRetries) dict for finer control. Tool retries cannot be overridden per run. See [`Agent.__init__`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.Agent.__init__) for semantics of the two enforcement paths. **`infer_name`** : [`bool`](https://docs.python.org/3/library/functions.html#bool) _Default:_ `True` Whether to try to infer the agent name from the call frame if it's not set. **`toolsets`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`AbstractToolset`](/docs/ai/api/pydantic-ai/toolsets/#pydantic_ai.toolsets.AbstractToolset)\[`AgentDepsT`\]\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional additional toolsets for this run. **`capabilities`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`AgentCapability`](/docs/ai/api/pydantic-ai/capabilities/#pydantic_ai.capabilities.AgentCapability)\[`AgentDepsT`\]\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional additional [capabilities](https://ai.pydantic.dev/capabilities/) for this run, merged with the agent's configured capabilities. **`spec`** : [`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] | `AgentSpec` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional agent spec to apply for this run. At run time, spec values are additive. ##### override ```python def override( name: str | _utils.Unset = _utils.UNSET, deps: AgentDepsT | _utils.Unset = _utils.UNSET, model: models.Model | models.KnownModelName | str | _utils.Unset = _utils.UNSET, toolsets: Sequence[AbstractToolset[AgentDepsT]] | _utils.Unset = _utils.UNSET, tools: Sequence[Tool[AgentDepsT] | ToolFuncEither[AgentDepsT, ...]] | _utils.Unset = _utils.UNSET, native_tools: Sequence[AgentNativeTool[AgentDepsT]] | _utils.Unset = _utils.UNSET, instructions: AgentInstructions[AgentDepsT] | _utils.Unset = _utils.UNSET, metadata: AgentMetadata[AgentDepsT] | _utils.Unset = _utils.UNSET, model_settings: AgentModelSettings[AgentDepsT] | _utils.Unset = _utils.UNSET, retries: int | AgentRetries | _utils.Unset = _utils.UNSET, spec: dict[str, Any] | AgentSpec | None = None, _deprecated_kwargs: Any = {}, ) -> Iterator[None] ``` Context manager to temporarily override agent configuration. This is particularly useful when testing. You can find an example of this [here](/docs/ai/guides/testing#overriding-model-via-pytest-fixtures). ###### Returns [`Iterator`](https://docs.python.org/3/library/typing.html#typing.Iterator)\[[`None`](https://docs.python.org/3/library/constants.html#None)\] ###### Parameters **`name`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | `_utils.Unset` _Default:_ `_utils.UNSET` The name to use instead of the name passed to the agent constructor and agent run. **`deps`** : `AgentDepsT` | `_utils.Unset` _Default:_ `_utils.UNSET` The dependencies to use instead of the dependencies passed to the agent run. **`model`** : [`models.Model`](/docs/ai/api/models/base/#pydantic_ai.models.Model) | [`models.KnownModelName`](/docs/ai/api/models/base/#pydantic_ai.models.KnownModelName) | [`str`](https://docs.python.org/3/library/stdtypes.html#str) | `_utils.Unset` _Default:_ `_utils.UNSET` The model to use instead of the model passed to the agent run. **`toolsets`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`AbstractToolset`](/docs/ai/api/pydantic-ai/toolsets/#pydantic_ai.toolsets.AbstractToolset)\[`AgentDepsT`\]\] | `_utils.Unset` _Default:_ `_utils.UNSET` The toolsets to use instead of the toolsets passed to the agent constructor and agent run. **`tools`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`Tool`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.Tool)\[`AgentDepsT`\] | `ToolFuncEither`\[`AgentDepsT`, ...\]\] | `_utils.Unset` _Default:_ `_utils.UNSET` The tools to use instead of the tools registered with the agent. **`native_tools`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`AgentNativeTool`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.AgentNativeTool)\[`AgentDepsT`\]\] | `_utils.Unset` _Default:_ `_utils.UNSET` The native tools to use instead of the agent's configured native tools. **`instructions`** : `AgentInstructions`\[`AgentDepsT`\] | `_utils.Unset` _Default:_ `_utils.UNSET` The instructions to use instead of the instructions registered with the agent. Note: this also replaces capability-contributed instructions (e.g. from [`get_instructions`](/docs/ai/api/pydantic-ai/capabilities/#pydantic_ai.capabilities.AbstractCapability.get_instructions)). **`metadata`** : `AgentMetadata`\[`AgentDepsT`\] | `_utils.Unset` _Default:_ `_utils.UNSET` The metadata to use instead of the metadata passed to the agent constructor. When set, any per-run `metadata` argument is ignored. **`model_settings`** : `AgentModelSettings`\[`AgentDepsT`\] | `_utils.Unset` _Default:_ `_utils.UNSET` The model settings to use instead of the model settings passed to the agent constructor. When set, any per-run `model_settings` argument is ignored. **`retries`** : [`int`](https://docs.python.org/3/library/functions.html#int) | [`AgentRetries`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.AgentRetries) | `_utils.Unset` _Default:_ `_utils.UNSET` The retry budgets to use instead of the agent-level configuration. Pass an `int` to override the output-validation budget, or an [`AgentRetries`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.AgentRetries) dict for finer control. When set, any per-run `retries` argument is ignored. Tool retries cannot be overridden via `override()`. **`spec`** : [`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] | `AgentSpec` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional agent spec providing defaults for override. Explicit params take precedence over spec values. When the spec includes `capabilities`, they replace (not merge with) the agent's existing capabilities. To add capabilities without replacing, pass `spec` to `run()` or `iter()` instead. ##### instructions ```python def instructions( func: Callable[[RunContext[AgentDepsT]], str | None], ) -> Callable[[RunContext[AgentDepsT]], str | None] def instructions( func: Callable[[RunContext[AgentDepsT]], Awaitable[str | None]], ) -> Callable[[RunContext[AgentDepsT]], Awaitable[str | None]] def instructions(func: Callable[[], str | None]) -> Callable[[], str | None] def instructions( func: Callable[[], Awaitable[str | None]], ) -> Callable[[], Awaitable[str | None]] def instructions( ) -> Callable[[_system_prompt.SystemPromptFunc[AgentDepsT]], _system_prompt.SystemPromptFunc[AgentDepsT]] ``` Decorator to register an instructions function. Optionally takes [`RunContext`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.RunContext) as its only argument. Can decorate a sync or async functions. The decorator can be used bare (`agent.instructions`). Overloads for every possible signature of `instructions` are included so the decorator doesn't obscure the type of the function. Example: ```python from pydantic_ai import Agent, RunContext agent = Agent('test', deps_type=str) @agent.instructions def simple_instructions() -> str: return 'foobar' @agent.instructions async def async_instructions(ctx: RunContext[str]) -> str: return f'{ctx.deps} is the best' ``` ###### Returns [`Callable`](https://docs.python.org/3/library/typing.html#typing.Callable)\[\[`_system_prompt.SystemPromptFunc`\[`AgentDepsT`\]\], `_system_prompt.SystemPromptFunc`\[`AgentDepsT`\]\] | `_system_prompt.SystemPromptFunc`\[`AgentDepsT`\] ##### system\_prompt\_parts `@async` ```python def system_prompt_parts( deps: AgentDepsT = None, model: models.Model | models.KnownModelName | str | None = None, message_history: Sequence[_messages.ModelMessage] | None = None, prompt: str | Sequence[_messages.UserContent] | None = None, usage: _usage.RunUsage | None = None, model_settings: ModelSettings | None = None, ) -> list[_messages.SystemPromptPart] ``` Resolve the agent's configured system prompts into `SystemPromptPart`s. See [`AbstractAgent.system_prompt_parts`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.AbstractAgent.system_prompt_parts). ###### Returns [`list`](https://docs.python.org/3/glossary.html#term-list)\[`_messages.SystemPromptPart`\] ##### system\_prompt ```python def system_prompt( func: Callable[[RunContext[AgentDepsT]], str | None], ) -> Callable[[RunContext[AgentDepsT]], str | None] def system_prompt( func: Callable[[RunContext[AgentDepsT]], Awaitable[str | None]], ) -> Callable[[RunContext[AgentDepsT]], Awaitable[str | None]] def system_prompt(func: Callable[[], str | None]) -> Callable[[], str | None] def system_prompt( func: Callable[[], Awaitable[str | None]], ) -> Callable[[], Awaitable[str | None]] def system_prompt( dynamic: bool = False, ) -> Callable[[_system_prompt.SystemPromptFunc[AgentDepsT]], _system_prompt.SystemPromptFunc[AgentDepsT]] ``` Decorator to register a system prompt function. Optionally takes [`RunContext`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.RunContext) as its only argument. Can decorate a sync or async functions. The decorator can be used either bare (`agent.system_prompt`) or as a function call (`agent.system_prompt(...)`), see the examples below. Overloads for every possible signature of `system_prompt` are included so the decorator doesn't obscure the type of the function, see `tests/typed_agent.py` for tests. Example: ```python from pydantic_ai import Agent, RunContext agent = Agent('test', deps_type=str) @agent.system_prompt def simple_system_prompt() -> str: return 'foobar' @agent.system_prompt(dynamic=True) async def async_system_prompt(ctx: RunContext[str]) -> str: return f'{ctx.deps} is the best' ``` ###### Returns [`Callable`](https://docs.python.org/3/library/typing.html#typing.Callable)\[\[`_system_prompt.SystemPromptFunc`\[`AgentDepsT`\]\], `_system_prompt.SystemPromptFunc`\[`AgentDepsT`\]\] | `_system_prompt.SystemPromptFunc`\[`AgentDepsT`\] ###### Parameters **`func`** : `_system_prompt.SystemPromptFunc`\[`AgentDepsT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` The function to decorate **`dynamic`** : [`bool`](https://docs.python.org/3/library/functions.html#bool) _Default:_ `False` If True, the system prompt will be reevaluated even when `messages_history` is provided, see [`SystemPromptPart.dynamic_ref`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.SystemPromptPart.dynamic_ref) ##### output\_validator ```python def output_validator( func: Callable[[RunContext[AgentDepsT], OutputDataT], OutputDataT], ) -> Callable[[RunContext[AgentDepsT], OutputDataT], OutputDataT] def output_validator( func: Callable[[RunContext[AgentDepsT], OutputDataT], Awaitable[OutputDataT]], ) -> Callable[[RunContext[AgentDepsT], OutputDataT], Awaitable[OutputDataT]] def output_validator( func: Callable[[OutputDataT], OutputDataT], ) -> Callable[[OutputDataT], OutputDataT] def output_validator( func: Callable[[OutputDataT], Awaitable[OutputDataT]], ) -> Callable[[OutputDataT], Awaitable[OutputDataT]] ``` Decorator to register an output validator function. Optionally takes [`RunContext`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.RunContext) as its first argument. Can decorate a sync or async functions. Overloads for every possible signature of `output_validator` are included so the decorator doesn't obscure the type of the function, see `tests/typed_agent.py` for tests. Example: ```python from pydantic_ai import Agent, ModelRetry, RunContext agent = Agent('test', deps_type=str) @agent.output_validator def output_validator_simple(data: str) -> str: if 'wrong' in data: raise ModelRetry('wrong response') return data @agent.output_validator async def output_validator_deps(ctx: RunContext[str], data: str) -> str: if ctx.deps in data: raise ModelRetry('wrong response') return data result = agent.run_sync('foobar', deps='spam') print(result.output) #> success (no tool calls) ``` ###### Returns `_output.OutputValidatorFunc`\[`AgentDepsT`, `OutputDataT`\] ##### tool ```python def tool( func: ToolFuncContext[AgentDepsT, ToolParams], ) -> ToolFuncContext[AgentDepsT, ToolParams] def tool( name: str | None = None, description: str | None = None, retries: int | None = None, prepare: ToolPrepareFunc[AgentDepsT] | None = None, args_validator: ArgsValidatorFunc[AgentDepsT, ToolParams] | None = None, docstring_format: DocstringFormat = 'auto', require_parameter_descriptions: bool = False, schema_generator: type[GenerateJsonSchema] = GenerateToolJsonSchema, strict: bool | None = None, sequential: bool = False, requires_approval: bool = False, metadata: dict[str, Any] | None = None, timeout: float | None = None, defer_loading: bool = False, include_return_schema: bool | None = None, ) -> Callable[[ToolFuncContext[AgentDepsT, ToolParams]], ToolFuncContext[AgentDepsT, ToolParams]] ``` Decorator to register a tool function which takes [`RunContext`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.RunContext) as its first argument. Can decorate a sync or async functions. The docstring is inspected to extract both the tool description and description of each parameter, [learn more](/docs/ai/tools-toolsets/tools#function-tools-and-schema). We can't add overloads for every possible signature of tool, since the return type is a recursive union so the signature of functions decorated with `@agent.tool` is obscured. Example: ```python from pydantic_ai import Agent, RunContext agent = Agent('test', deps_type=int) @agent.tool def foobar(ctx: RunContext[int], x: int) -> int: return ctx.deps + x @agent.tool(retries=2) async def spam(ctx: RunContext[str], y: float) -> float: return ctx.deps + y result = agent.run_sync('foobar', deps=1) print(result.output) #> {"foobar":1,"spam":1.0} ``` ###### Returns [`Any`](https://docs.python.org/3/library/typing.html#typing.Any) ###### Parameters **`func`** : `ToolFuncContext`\[`AgentDepsT`, `ToolParams`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` The tool function to register. **`name`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` The name of the tool, defaults to the function name. **`description`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` The description of the tool, defaults to the function docstring. **`retries`** : [`int`](https://docs.python.org/3/library/functions.html#int) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` The number of retries to allow for this tool, defaults to the agent's default retries, which defaults to 1. **`prepare`** : `ToolPrepareFunc`\[`AgentDepsT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` custom method to prepare the tool definition for each step, return `None` to omit this tool from a given step. This is useful if you want to customise a tool at call time, or omit it completely from a step. See [`ToolPrepareFunc`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.ToolPrepareFunc). **`args_validator`** : `ArgsValidatorFunc`\[`AgentDepsT`, `ToolParams`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` custom method to validate tool arguments after schema validation has passed, before execution. The validator receives the already-validated and type-converted parameters, with `RunContext` as the first argument. Should raise [`ModelRetry`](/docs/ai/api/pydantic-ai/exceptions/#pydantic_ai.exceptions.ModelRetry) on validation failure, return `None` on success. See [`ArgsValidatorFunc`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.ArgsValidatorFunc). **`docstring_format`** : `DocstringFormat` _Default:_ `'auto'` The format of the docstring, see [`DocstringFormat`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.DocstringFormat). Defaults to `'auto'`, such that the format is inferred from the structure of the docstring. **`require_parameter_descriptions`** : [`bool`](https://docs.python.org/3/library/functions.html#bool) _Default:_ `False` If True, raise an error if a parameter description is missing. Defaults to False. **`schema_generator`** : [`type`](https://docs.python.org/3/glossary.html#term-type)\[`GenerateJsonSchema`\] _Default:_ `GenerateToolJsonSchema` The JSON schema generator class to use for this tool. Defaults to `GenerateToolJsonSchema`. **`strict`** : [`bool`](https://docs.python.org/3/library/functions.html#bool) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Whether to enforce JSON schema compliance (only affects OpenAI). See [`ToolDefinition`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.ToolDefinition) for more info. **`sequential`** : [`bool`](https://docs.python.org/3/library/functions.html#bool) _Default:_ `False` Whether the function requires a sequential/serial execution environment. Defaults to False. **`requires_approval`** : [`bool`](https://docs.python.org/3/library/functions.html#bool) _Default:_ `False` Whether this tool requires human-in-the-loop approval. Defaults to False. See the [tools documentation](/docs/ai/tools-toolsets/deferred-tools#human-in-the-loop-tool-approval) for more info. **`metadata`** : [`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional metadata for the tool. This is not sent to the model but can be used for filtering and tool behavior customization. **`timeout`** : [`float`](https://docs.python.org/3/library/functions.html#float) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Timeout in seconds for tool execution. If the tool takes longer, a retry prompt is returned to the model. Overrides the agent-level `tool_timeout` if set. Defaults to None (no timeout). **`defer_loading`** : [`bool`](https://docs.python.org/3/library/functions.html#bool) _Default:_ `False` Whether to hide this tool until it's discovered via tool search. Defaults to False. See [Tool Search](/docs/ai/tools-toolsets/tools-advanced#tool-search) for more info. **`include_return_schema`** : [`bool`](https://docs.python.org/3/library/functions.html#bool) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Whether to include the return schema in the tool definition sent to the model. If `None`, defaults to `False` unless the [`IncludeToolReturnSchemas`](/docs/ai/api/pydantic-ai/capabilities/#pydantic_ai.capabilities.IncludeToolReturnSchemas) capability is used. ##### tool\_plain ```python def tool_plain(func: ToolFuncPlain[ToolParams]) -> ToolFuncPlain[ToolParams] def tool_plain( name: str | None = None, description: str | None = None, retries: int | None = None, prepare: ToolPrepareFunc[AgentDepsT] | None = None, args_validator: ArgsValidatorFunc[AgentDepsT, ToolParams] | None = None, docstring_format: DocstringFormat = 'auto', require_parameter_descriptions: bool = False, schema_generator: type[GenerateJsonSchema] = GenerateToolJsonSchema, strict: bool | None = None, sequential: bool = False, requires_approval: bool = False, metadata: dict[str, Any] | None = None, timeout: float | None = None, defer_loading: bool = False, include_return_schema: bool | None = None, ) -> Callable[[ToolFuncPlain[ToolParams]], ToolFuncPlain[ToolParams]] ``` Decorator to register a tool function which DOES NOT take `RunContext` as an argument. Can decorate a sync or async functions. The docstring is inspected to extract both the tool description and description of each parameter, [learn more](/docs/ai/tools-toolsets/tools#function-tools-and-schema). We can't add overloads for every possible signature of tool, since the return type is a recursive union so the signature of functions decorated with `@agent.tool` is obscured. Example: ```python from pydantic_ai import Agent, RunContext agent = Agent('test') @agent.tool def foobar(ctx: RunContext[int]) -> int: return 123 @agent.tool(retries=2) async def spam(ctx: RunContext[str]) -> float: return 3.14 result = agent.run_sync('foobar', deps=1) print(result.output) #> {"foobar":123,"spam":3.14} ``` ###### Returns [`Any`](https://docs.python.org/3/library/typing.html#typing.Any) ###### Parameters **`func`** : `ToolFuncPlain`\[`ToolParams`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` The tool function to register. **`name`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` The name of the tool, defaults to the function name. **`description`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` The description of the tool, defaults to the function docstring. **`retries`** : [`int`](https://docs.python.org/3/library/functions.html#int) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` The number of retries to allow for this tool, defaults to the agent's default retries, which defaults to 1. **`prepare`** : `ToolPrepareFunc`\[`AgentDepsT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` custom method to prepare the tool definition for each step, return `None` to omit this tool from a given step. This is useful if you want to customise a tool at call time, or omit it completely from a step. See [`ToolPrepareFunc`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.ToolPrepareFunc). **`args_validator`** : `ArgsValidatorFunc`\[`AgentDepsT`, `ToolParams`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` custom method to validate tool arguments after schema validation has passed, before execution. The validator receives the already-validated and type-converted parameters, with [`RunContext`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.RunContext) as the first argument -- even though the tool function itself does not take `RunContext` when using `tool_plain`. Should raise [`ModelRetry`](/docs/ai/api/pydantic-ai/exceptions/#pydantic_ai.exceptions.ModelRetry) on validation failure, return `None` on success. See [`ArgsValidatorFunc`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.ArgsValidatorFunc). **`docstring_format`** : `DocstringFormat` _Default:_ `'auto'` The format of the docstring, see [`DocstringFormat`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.DocstringFormat). Defaults to `'auto'`, such that the format is inferred from the structure of the docstring. **`require_parameter_descriptions`** : [`bool`](https://docs.python.org/3/library/functions.html#bool) _Default:_ `False` If True, raise an error if a parameter description is missing. Defaults to False. **`schema_generator`** : [`type`](https://docs.python.org/3/glossary.html#term-type)\[`GenerateJsonSchema`\] _Default:_ `GenerateToolJsonSchema` The JSON schema generator class to use for this tool. Defaults to `GenerateToolJsonSchema`. **`strict`** : [`bool`](https://docs.python.org/3/library/functions.html#bool) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Whether to enforce JSON schema compliance (only affects OpenAI). See [`ToolDefinition`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.ToolDefinition) for more info. **`sequential`** : [`bool`](https://docs.python.org/3/library/functions.html#bool) _Default:_ `False` Whether the function requires a sequential/serial execution environment. Defaults to False. **`requires_approval`** : [`bool`](https://docs.python.org/3/library/functions.html#bool) _Default:_ `False` Whether this tool requires human-in-the-loop approval. Defaults to False. See the [tools documentation](/docs/ai/tools-toolsets/deferred-tools#human-in-the-loop-tool-approval) for more info. **`metadata`** : [`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional metadata for the tool. This is not sent to the model but can be used for filtering and tool behavior customization. **`timeout`** : [`float`](https://docs.python.org/3/library/functions.html#float) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Timeout in seconds for tool execution. If the tool takes longer, a retry prompt is returned to the model. Overrides the agent-level `tool_timeout` if set. Defaults to None (no timeout). **`defer_loading`** : [`bool`](https://docs.python.org/3/library/functions.html#bool) _Default:_ `False` Whether to hide this tool until it's discovered via tool search. Defaults to False. See [Tool Search](/docs/ai/tools-toolsets/tools-advanced#tool-search) for more info. **`include_return_schema`** : [`bool`](https://docs.python.org/3/library/functions.html#bool) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Whether to include the return schema in the tool definition sent to the model. If `None`, defaults to `False` unless the [`IncludeToolReturnSchemas`](/docs/ai/api/pydantic-ai/capabilities/#pydantic_ai.capabilities.IncludeToolReturnSchemas) capability is used. ##### toolset ```python def toolset(func: ToolsetFunc[AgentDepsT]) -> ToolsetFunc[AgentDepsT] def toolset( per_run_step: bool = True, id: str | None = None, ) -> Callable[[ToolsetFunc[AgentDepsT]], ToolsetFunc[AgentDepsT]] ``` Decorator to register a toolset function which takes [`RunContext`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.RunContext) as its only argument. Can decorate a sync or async functions. The decorator can be used bare (`agent.toolset`). Example: ```python from pydantic_ai import AbstractToolset, Agent, FunctionToolset, RunContext agent = Agent('test', deps_type=str) @agent.toolset async def simple_toolset(ctx: RunContext[str]) -> AbstractToolset[str]: return FunctionToolset() ``` ###### Returns [`Any`](https://docs.python.org/3/library/typing.html#typing.Any) ###### Parameters **`func`** : [`ToolsetFunc`](/docs/ai/api/pydantic-ai/toolsets/#pydantic_ai.toolsets.ToolsetFunc)\[`AgentDepsT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` The toolset function to register. **`per_run_step`** : [`bool`](https://docs.python.org/3/library/functions.html#bool) _Default:_ `True` Whether to re-evaluate the toolset for each run step. Defaults to True. **`id`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` An optional unique ID for the dynamic toolset. Required for use with durable execution environments like Temporal, where the ID identifies the toolset's activities within the workflow. ##### \_\_aenter\_\_ `@async` ```python def __aenter__() -> Self ``` Enter the agent context. This will start all [`MCPServerStdio`s](/docs/ai/api/pydantic-ai/mcp/#pydantic_ai.mcp.MCPServerStdio) registered as `toolsets` so they are ready to be used, and enter the model so the provider's HTTP client will be closed cleanly on exit. This is a no-op if the agent has already been entered. ###### Returns [`Self`](https://docs.python.org/3/library/typing.html#typing.Self) ##### set\_mcp\_sampling\_model ```python def set_mcp_sampling_model( model: models.Model | models.KnownModelName | str | None = None, ) -> None ``` Set the sampling model on all MCP servers registered with the agent. If no sampling model is provided, the agent's model will be used. ###### Returns [`None`](https://docs.python.org/3/library/constants.html#None) ##### to\_web ```python def to_web( models: ModelsParam = None, deps: AgentDepsT = None, model_settings: ModelSettings | None = None, instructions: str | None = None, html_source: str | Path | None = None, _deprecated_kwargs: Any = {}, ) -> Starlette ``` Create a Starlette app that serves a web chat UI for this agent. This method returns a pre-configured Starlette application that provides a web-based chat interface for interacting with the agent. By default, the UI is fetched from a CDN and cached on first use. The returned Starlette application can be mounted into a FastAPI app or run directly with any ASGI server (uvicorn, hypercorn, etc.). Note that the `deps` and `model_settings` will be the same for each request. To provide different `deps` for each request use the lower-level adapters directly. The agent's configured native tools (registered via `capabilities=[NativeTool(...)]` or higher-level capabilities like `WebSearch()`) are automatically exposed as options in the UI. ###### Returns `Starlette` -- A configured Starlette application ready to be served (e.g., with uvicorn) ###### Parameters **`models`** : `ModelsParam` _Default:_ `None` Additional models to make available in the UI. Can be: - A sequence of model names/instances (e.g., `['openai:gpt-5', 'anthropic:claude-sonnet-4-6']`) - A dict mapping display labels to model names/instances (e.g., `{'GPT 5': 'openai:gpt-5', 'Claude': 'anthropic:claude-sonnet-4-6'}`) The agent's model is always included. Native tool support is automatically determined from each model's profile. **`deps`** : `AgentDepsT` _Default:_ `None` Optional dependencies to use for all requests. **`model_settings`** : [`ModelSettings`](/docs/ai/api/pydantic-ai/settings/#pydantic_ai.settings.ModelSettings) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional settings to use for all model requests. **`instructions`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional extra instructions to pass to each agent run. **`html_source`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | `Path` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Path or URL for the chat UI HTML. Can be: - None (default): Fetches from CDN and caches locally - A Path instance: Reads from the local file - A URL string (http:// or https://): Fetches from the URL - A file path string: Reads from the local file ##### run\_mcp\_servers `@async` `@deprecated` ```python def run_mcp_servers( model: models.Model | models.KnownModelName | str | None = None, ) -> AsyncIterator[None] ``` Run [`MCPServerStdio`s](/docs/ai/api/pydantic-ai/mcp/#pydantic_ai.mcp.MCPServerStdio) so they can be used by the agent. Deprecated: use [`async with agent`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.Agent.__aenter__) instead. If you need to set a sampling model on all MCP servers, use [`agent.set_mcp_sampling_model()`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.Agent.set_mcp_sampling_model). Returns: a context manager to start and shutdown the servers. ###### Returns [`AsyncIterator`](https://docs.python.org/3/library/typing.html#typing.AsyncIterator)\[[`None`](https://docs.python.org/3/library/constants.html#None)\] ### AbstractAgent **Bases:** `Generic[AgentDepsT, OutputDataT]`, `ABC` Abstract superclass for [`Agent`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.Agent), [`WrapperAgent`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.WrapperAgent), and your own custom agent implementations. #### Attributes ##### model The default model configured for this agent. **Type:** [`models.Model`](/docs/ai/api/models/base/#pydantic_ai.models.Model) | [`models.KnownModelName`](/docs/ai/api/models/base/#pydantic_ai.models.KnownModelName) | [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) ##### name The name of the agent, used for logging. If `None`, we try to infer the agent name from the call frame when the agent is first run. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) ##### description A human-readable description of the agent. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) ##### deps\_type The type of dependencies used by the agent. **Type:** [`type`](https://docs.python.org/3/glossary.html#term-type) ##### output\_type The type of data output by agent runs, used to validate the data returned by the model, defaults to `str`. **Type:** `OutputSpec`\[`OutputDataT`\] ##### event\_stream\_handler Optional handler for events from the model's streaming response and the agent's execution of tools. **Type:** `EventStreamHandler`\[`AgentDepsT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) ##### root\_capability The root capability of the agent, containing all registered capabilities. **Type:** `CombinedCapability`\[`AgentDepsT`\] ##### toolsets All toolsets registered on the agent. Output tools are not included. **Type:** [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`AbstractToolset`](/docs/ai/api/pydantic-ai/toolsets/#pydantic_ai.toolsets.AbstractToolset)\[`AgentDepsT`\]\] #### Methods ##### system\_prompt\_parts `@async` ```python def system_prompt_parts( deps: AgentDepsT = None, model: models.Model | models.KnownModelName | str | None = None, message_history: Sequence[_messages.ModelMessage] | None = None, prompt: str | Sequence[_messages.UserContent] | None = None, usage: _usage.RunUsage | None = None, model_settings: ModelSettings | None = None, ) -> list[_messages.SystemPromptPart] ``` Resolve the agent's configured system prompts into `SystemPromptPart`s. Returns a list suitable for prepending to a `ModelRequest`. Static strings and runners decorated with [`@agent.system_prompt`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.Agent.system_prompt) are evaluated using a minimal `RunContext` built from the provided kwargs -- useful when reconstructing a `message_history` that should carry the agent's configured system prompt (e.g. in UI adapters or after history compaction). Dynamic runners produce parts with `dynamic_ref` set so they can continue to be re-evaluated by the standard agent graph path on subsequent turns. ###### Returns [`list`](https://docs.python.org/3/glossary.html#term-list)\[`_messages.SystemPromptPart`\] ###### Parameters **`deps`** : `AgentDepsT` _Default:_ `None` Optional dependencies for dynamic system prompt functions. **`model`** : [`models.Model`](/docs/ai/api/models/base/#pydantic_ai.models.Model) | [`models.KnownModelName`](/docs/ai/api/models/base/#pydantic_ai.models.KnownModelName) | [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional model to use for `RunContext.model`. Falls back to the agent's configured model; required only if the agent has no model set. **`message_history`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[`_messages.ModelMessage`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional message history to expose as `RunContext.messages`. **`prompt`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[`_messages.UserContent`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional user prompt to expose as `RunContext.prompt`. **`usage`** : `_usage.RunUsage` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional usage to expose as `RunContext.usage`. **`model_settings`** : [`ModelSettings`](/docs/ai/api/pydantic-ai/settings/#pydantic_ai.settings.ModelSettings) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional settings to expose as `RunContext.model_settings`. ##### output\_json\_schema ```python def output_json_schema( output_type: OutputSpec[OutputDataT | RunOutputDataT] | None = None, ) -> JsonSchema ``` The output return JSON schema. ###### Returns `JsonSchema` ##### run `@async` ```python def run( user_prompt: str | Sequence[_messages.UserContent] | None = None, output_type: None = None, message_history: Sequence[_messages.ModelMessage] | None = None, deferred_tool_results: DeferredToolResults | None = None, conversation_id: str | None = None, model: models.Model | models.KnownModelName | str | None = None, instructions: _instructions.AgentInstructions[AgentDepsT] = None, deps: AgentDepsT = None, model_settings: AgentModelSettings[AgentDepsT] | None = None, usage_limits: _usage.UsageLimits | None = None, usage: _usage.RunUsage | None = None, metadata: AgentMetadata[AgentDepsT] | None = None, retries: int | AgentRetries | None = None, infer_name: bool = True, toolsets: Sequence[AbstractToolset[AgentDepsT]] | None = None, event_stream_handler: EventStreamHandler[AgentDepsT] | None = None, capabilities: Sequence[AgentCapability[AgentDepsT]] | None = None, spec: dict[str, Any] | AgentSpec | None = None, ) -> AgentRunResult[OutputDataT] def run( user_prompt: str | Sequence[_messages.UserContent] | None = None, output_type: OutputSpec[RunOutputDataT], message_history: Sequence[_messages.ModelMessage] | None = None, deferred_tool_results: DeferredToolResults | None = None, conversation_id: str | None = None, model: models.Model | models.KnownModelName | str | None = None, instructions: _instructions.AgentInstructions[AgentDepsT] = None, deps: AgentDepsT = None, model_settings: AgentModelSettings[AgentDepsT] | None = None, usage_limits: _usage.UsageLimits | None = None, usage: _usage.RunUsage | None = None, metadata: AgentMetadata[AgentDepsT] | None = None, retries: int | AgentRetries | None = None, infer_name: bool = True, toolsets: Sequence[AbstractToolset[AgentDepsT]] | None = None, event_stream_handler: EventStreamHandler[AgentDepsT] | None = None, capabilities: Sequence[AgentCapability[AgentDepsT]] | None = None, spec: dict[str, Any] | AgentSpec | None = None, ) -> AgentRunResult[RunOutputDataT] ``` Run the agent with a user prompt in async mode. This method builds an internal agent graph (using system prompts, tools and output schemas) and then runs the graph to completion. The result of the run is returned. Example: ```python from pydantic_ai import Agent agent = Agent('openai:gpt-5.2') async def main(): agent_run = await agent.run('What is the capital of France?') print(agent_run.output) #> The capital of France is Paris. ``` ###### Returns [`AgentRunResult`](/docs/ai/api/pydantic-ai/run/#pydantic_ai.run.AgentRunResult)\[[`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] -- The result of the run. ###### Parameters **`user_prompt`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[`_messages.UserContent`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` User input to start/continue the conversation. **`output_type`** : `OutputSpec`\[`RunOutputDataT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Custom output type to use for this run, `output_type` may only be used if the agent has no output validators since output validators would expect an argument that matches the agent's output type. **`message_history`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[`_messages.ModelMessage`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` History of the conversation so far. **`deferred_tool_results`** : [`DeferredToolResults`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.DeferredToolResults) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional results for deferred tool calls in the message history. **`conversation_id`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` ID of the conversation this run belongs to. Pass `'new'` to start a fresh conversation, ignoring any `conversation_id` already on `message_history`. If omitted, falls back to the most recent `conversation_id` on `message_history` or a freshly generated UUID7. **`model`** : [`models.Model`](/docs/ai/api/models/base/#pydantic_ai.models.Model) | [`models.KnownModelName`](/docs/ai/api/models/base/#pydantic_ai.models.KnownModelName) | [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional model to use for this run, required if `model` was not set when creating the agent. **`instructions`** : `_instructions.AgentInstructions`\[`AgentDepsT`\] _Default:_ `None` Optional additional instructions to use for this run. **`deps`** : `AgentDepsT` _Default:_ `None` Optional dependencies to use for this run. **`model_settings`** : `AgentModelSettings`\[`AgentDepsT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional settings to use for this model's request, or a callable that receives [`RunContext`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.RunContext) and returns settings. Callables are called before each model request, allowing dynamic per-step settings. **`usage_limits`** : `_usage.UsageLimits` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional limits on model request count or token usage. **`usage`** : `_usage.RunUsage` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional usage to start with, useful for resuming a conversation or agents used in tools. **`metadata`** : `AgentMetadata`\[`AgentDepsT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional metadata to attach to this run. Accepts a dictionary or a callable taking [`RunContext`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.RunContext); merged with the agent's configured metadata. **`retries`** : [`int`](https://docs.python.org/3/library/functions.html#int) | [`AgentRetries`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.AgentRetries) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Override the agent-level retry budgets for this run. Pass an `int` to override the output-validation budget (`AgentRetries(output=...)` equivalent), or an [`AgentRetries`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.AgentRetries) dict for finer control. Tool retries cannot be overridden per run. See [`Agent.__init__`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.Agent.__init__) for semantics of the two enforcement paths. **`infer_name`** : [`bool`](https://docs.python.org/3/library/functions.html#bool) _Default:_ `True` Whether to try to infer the agent name from the call frame if it's not set. **`toolsets`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`AbstractToolset`](/docs/ai/api/pydantic-ai/toolsets/#pydantic_ai.toolsets.AbstractToolset)\[`AgentDepsT`\]\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional additional toolsets for this run. **`event_stream_handler`** : `EventStreamHandler`\[`AgentDepsT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional handler for events from the model's streaming response and the agent's execution of tools to use for this run. **`capabilities`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`AgentCapability`](/docs/ai/api/pydantic-ai/capabilities/#pydantic_ai.capabilities.AgentCapability)\[`AgentDepsT`\]\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional additional [capabilities](https://ai.pydantic.dev/capabilities/) for this run, merged with the agent's configured capabilities. **`spec`** : [`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] | `AgentSpec` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional agent spec to apply for this run. At run time, spec values are additive. ##### run\_sync ```python def run_sync( user_prompt: str | Sequence[_messages.UserContent] | None = None, output_type: None = None, message_history: Sequence[_messages.ModelMessage] | None = None, deferred_tool_results: DeferredToolResults | None = None, conversation_id: str | None = None, model: models.Model | models.KnownModelName | str | None = None, instructions: _instructions.AgentInstructions[AgentDepsT] = None, deps: AgentDepsT = None, model_settings: AgentModelSettings[AgentDepsT] | None = None, usage_limits: _usage.UsageLimits | None = None, usage: _usage.RunUsage | None = None, metadata: AgentMetadata[AgentDepsT] | None = None, retries: int | AgentRetries | None = None, infer_name: bool = True, toolsets: Sequence[AbstractToolset[AgentDepsT]] | None = None, event_stream_handler: EventStreamHandler[AgentDepsT] | None = None, capabilities: Sequence[AgentCapability[AgentDepsT]] | None = None, spec: dict[str, Any] | AgentSpec | None = None, ) -> AgentRunResult[OutputDataT] def run_sync( user_prompt: str | Sequence[_messages.UserContent] | None = None, output_type: OutputSpec[RunOutputDataT], message_history: Sequence[_messages.ModelMessage] | None = None, deferred_tool_results: DeferredToolResults | None = None, conversation_id: str | None = None, model: models.Model | models.KnownModelName | str | None = None, instructions: _instructions.AgentInstructions[AgentDepsT] = None, deps: AgentDepsT = None, model_settings: AgentModelSettings[AgentDepsT] | None = None, usage_limits: _usage.UsageLimits | None = None, usage: _usage.RunUsage | None = None, metadata: AgentMetadata[AgentDepsT] | None = None, retries: int | AgentRetries | None = None, infer_name: bool = True, toolsets: Sequence[AbstractToolset[AgentDepsT]] | None = None, event_stream_handler: EventStreamHandler[AgentDepsT] | None = None, capabilities: Sequence[AgentCapability[AgentDepsT]] | None = None, spec: dict[str, Any] | AgentSpec | None = None, ) -> AgentRunResult[RunOutputDataT] ``` Synchronously run the agent with a user prompt. This is a convenience method that wraps [`self.run`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.AbstractAgent.run) with `loop.run_until_complete(...)`. You therefore can't use this method inside async code or if there's an active event loop. Example: ```python from pydantic_ai import Agent agent = Agent('openai:gpt-5.2') result_sync = agent.run_sync('What is the capital of Italy?') print(result_sync.output) #> The capital of Italy is Rome. ``` ###### Returns [`AgentRunResult`](/docs/ai/api/pydantic-ai/run/#pydantic_ai.run.AgentRunResult)\[[`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] -- The result of the run. ###### Parameters **`user_prompt`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[`_messages.UserContent`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` User input to start/continue the conversation. **`output_type`** : `OutputSpec`\[`RunOutputDataT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Custom output type to use for this run, `output_type` may only be used if the agent has no output validators since output validators would expect an argument that matches the agent's output type. **`message_history`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[`_messages.ModelMessage`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` History of the conversation so far. **`deferred_tool_results`** : [`DeferredToolResults`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.DeferredToolResults) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional results for deferred tool calls in the message history. **`conversation_id`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` ID of the conversation this run belongs to. Pass `'new'` to start a fresh conversation, ignoring any `conversation_id` already on `message_history`. If omitted, falls back to the most recent `conversation_id` on `message_history` or a freshly generated UUID7. **`model`** : [`models.Model`](/docs/ai/api/models/base/#pydantic_ai.models.Model) | [`models.KnownModelName`](/docs/ai/api/models/base/#pydantic_ai.models.KnownModelName) | [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional model to use for this run, required if `model` was not set when creating the agent. **`instructions`** : `_instructions.AgentInstructions`\[`AgentDepsT`\] _Default:_ `None` Optional additional instructions to use for this run. **`deps`** : `AgentDepsT` _Default:_ `None` Optional dependencies to use for this run. **`model_settings`** : `AgentModelSettings`\[`AgentDepsT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional settings to use for this model's request, or a callable that receives [`RunContext`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.RunContext) and returns settings. Callables are called before each model request, allowing dynamic per-step settings. **`usage_limits`** : `_usage.UsageLimits` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional limits on model request count or token usage. **`usage`** : `_usage.RunUsage` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional usage to start with, useful for resuming a conversation or agents used in tools. **`metadata`** : `AgentMetadata`\[`AgentDepsT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional metadata to attach to this run. Accepts a dictionary or a callable taking [`RunContext`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.RunContext); merged with the agent's configured metadata. **`retries`** : [`int`](https://docs.python.org/3/library/functions.html#int) | [`AgentRetries`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.AgentRetries) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Override the agent-level retry budgets for this run. Pass an `int` to override the output-validation budget (`AgentRetries(output=...)` equivalent), or an [`AgentRetries`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.AgentRetries) dict for finer control. Tool retries cannot be overridden per run. See [`Agent.__init__`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.Agent.__init__) for semantics of the two enforcement paths. **`infer_name`** : [`bool`](https://docs.python.org/3/library/functions.html#bool) _Default:_ `True` Whether to try to infer the agent name from the call frame if it's not set. **`toolsets`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`AbstractToolset`](/docs/ai/api/pydantic-ai/toolsets/#pydantic_ai.toolsets.AbstractToolset)\[`AgentDepsT`\]\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional additional toolsets for this run. **`event_stream_handler`** : `EventStreamHandler`\[`AgentDepsT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional handler for events from the model's streaming response and the agent's execution of tools to use for this run. **`capabilities`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`AgentCapability`](/docs/ai/api/pydantic-ai/capabilities/#pydantic_ai.capabilities.AgentCapability)\[`AgentDepsT`\]\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional additional [capabilities](https://ai.pydantic.dev/capabilities/) for this run, merged with the agent's configured capabilities. **`spec`** : [`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] | `AgentSpec` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional agent spec to apply for this run. At run time, spec values are additive. ##### run\_stream `@async` ```python def run_stream( user_prompt: str | Sequence[_messages.UserContent] | None = None, output_type: None = None, message_history: Sequence[_messages.ModelMessage] | None = None, deferred_tool_results: DeferredToolResults | None = None, conversation_id: str | None = None, model: models.Model | models.KnownModelName | str | None = None, instructions: _instructions.AgentInstructions[AgentDepsT] = None, deps: AgentDepsT = None, model_settings: AgentModelSettings[AgentDepsT] | None = None, usage_limits: _usage.UsageLimits | None = None, usage: _usage.RunUsage | None = None, metadata: AgentMetadata[AgentDepsT] | None = None, retries: int | AgentRetries | None = None, infer_name: bool = True, toolsets: Sequence[AbstractToolset[AgentDepsT]] | None = None, event_stream_handler: EventStreamHandler[AgentDepsT] | None = None, capabilities: Sequence[AgentCapability[AgentDepsT]] | None = None, spec: dict[str, Any] | AgentSpec | None = None, ) -> AbstractAsyncContextManager[result.StreamedRunResult[AgentDepsT, OutputDataT]] def run_stream( user_prompt: str | Sequence[_messages.UserContent] | None = None, output_type: OutputSpec[RunOutputDataT], message_history: Sequence[_messages.ModelMessage] | None = None, deferred_tool_results: DeferredToolResults | None = None, conversation_id: str | None = None, model: models.Model | models.KnownModelName | str | None = None, instructions: _instructions.AgentInstructions[AgentDepsT] = None, deps: AgentDepsT = None, model_settings: AgentModelSettings[AgentDepsT] | None = None, usage_limits: _usage.UsageLimits | None = None, usage: _usage.RunUsage | None = None, metadata: AgentMetadata[AgentDepsT] | None = None, retries: int | AgentRetries | None = None, infer_name: bool = True, toolsets: Sequence[AbstractToolset[AgentDepsT]] | None = None, event_stream_handler: EventStreamHandler[AgentDepsT] | None = None, capabilities: Sequence[AgentCapability[AgentDepsT]] | None = None, spec: dict[str, Any] | AgentSpec | None = None, ) -> AbstractAsyncContextManager[result.StreamedRunResult[AgentDepsT, RunOutputDataT]] ``` Run the agent with a user prompt in async streaming mode. This method builds an internal agent graph (using system prompts, tools and output schemas) and then runs the graph until the model produces output matching the `output_type`, for example text or structured data. At this point, a streaming run result object is yielded from which you can stream the output as it comes in, and -- once this output has completed streaming -- get the complete output, message history, and usage. As this method will consider the first output matching the `output_type` to be the final output, it will stop running the agent graph and will not execute any tool calls made by the model after this "final" output. If you want to always run the agent graph to completion and stream events and output at the same time, use [`agent.run()`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.AbstractAgent.run) with an `event_stream_handler` or [`agent.iter()`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.AbstractAgent.iter) instead. Example: ```python from pydantic_ai import Agent agent = Agent('openai:gpt-5.2') async def main(): async with agent.run_stream('What is the capital of the UK?') as response: print(await response.get_output()) #> The capital of the UK is London. ``` ###### Returns [`AsyncIterator`](https://docs.python.org/3/library/typing.html#typing.AsyncIterator)\[[`result.StreamedRunResult`](/docs/ai/api/pydantic-ai/result/#pydantic_ai.result.StreamedRunResult)\[`AgentDepsT`, [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\]\] -- The result of the run. ###### Parameters **`user_prompt`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[`_messages.UserContent`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` User input to start/continue the conversation. **`output_type`** : `OutputSpec`\[`RunOutputDataT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Custom output type to use for this run, `output_type` may only be used if the agent has no output validators since output validators would expect an argument that matches the agent's output type. **`message_history`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[`_messages.ModelMessage`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` History of the conversation so far. **`deferred_tool_results`** : [`DeferredToolResults`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.DeferredToolResults) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional results for deferred tool calls in the message history. **`conversation_id`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` ID of the conversation this run belongs to. Pass `'new'` to start a fresh conversation, ignoring any `conversation_id` already on `message_history`. If omitted, falls back to the most recent `conversation_id` on `message_history` or a freshly generated UUID7. **`model`** : [`models.Model`](/docs/ai/api/models/base/#pydantic_ai.models.Model) | [`models.KnownModelName`](/docs/ai/api/models/base/#pydantic_ai.models.KnownModelName) | [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional model to use for this run, required if `model` was not set when creating the agent. **`instructions`** : `_instructions.AgentInstructions`\[`AgentDepsT`\] _Default:_ `None` Optional additional instructions to use for this run. **`deps`** : `AgentDepsT` _Default:_ `None` Optional dependencies to use for this run. **`model_settings`** : `AgentModelSettings`\[`AgentDepsT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional settings to use for this model's request, or a callable that receives [`RunContext`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.RunContext) and returns settings. Callables are called before each model request, allowing dynamic per-step settings. **`usage_limits`** : `_usage.UsageLimits` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional limits on model request count or token usage. **`usage`** : `_usage.RunUsage` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional usage to start with, useful for resuming a conversation or agents used in tools. **`metadata`** : `AgentMetadata`\[`AgentDepsT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional metadata to attach to this run. Accepts a dictionary or a callable taking [`RunContext`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.RunContext); merged with the agent's configured metadata. **`retries`** : [`int`](https://docs.python.org/3/library/functions.html#int) | [`AgentRetries`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.AgentRetries) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Override the agent-level retry budgets for this run. Pass an `int` to override the output-validation budget (`AgentRetries(output=...)` equivalent), or an [`AgentRetries`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.AgentRetries) dict for finer control. Tool retries cannot be overridden per run. See [`Agent.__init__`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.Agent.__init__) for semantics of the two enforcement paths. **`infer_name`** : [`bool`](https://docs.python.org/3/library/functions.html#bool) _Default:_ `True` Whether to try to infer the agent name from the call frame if it's not set. **`toolsets`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`AbstractToolset`](/docs/ai/api/pydantic-ai/toolsets/#pydantic_ai.toolsets.AbstractToolset)\[`AgentDepsT`\]\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional additional toolsets for this run. **`event_stream_handler`** : `EventStreamHandler`\[`AgentDepsT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional handler for events from the model's streaming response and the agent's execution of tools to use for this run. It will receive all the events up until the final result is found, which you can then read or stream from inside the context manager. Note that it does _not_ receive any events after the final result is found. **`capabilities`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`AgentCapability`](/docs/ai/api/pydantic-ai/capabilities/#pydantic_ai.capabilities.AgentCapability)\[`AgentDepsT`\]\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional additional [capabilities](https://ai.pydantic.dev/capabilities/) for this run, merged with the agent's configured capabilities. **`spec`** : [`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] | `AgentSpec` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional agent spec to apply for this run. At run time, spec values are additive. ##### run\_stream\_sync ```python def run_stream_sync( user_prompt: str | Sequence[_messages.UserContent] | None = None, output_type: None = None, message_history: Sequence[_messages.ModelMessage] | None = None, deferred_tool_results: DeferredToolResults | None = None, conversation_id: str | None = None, model: models.Model | models.KnownModelName | str | None = None, deps: AgentDepsT = None, model_settings: AgentModelSettings[AgentDepsT] | None = None, usage_limits: _usage.UsageLimits | None = None, usage: _usage.RunUsage | None = None, metadata: AgentMetadata[AgentDepsT] | None = None, retries: int | AgentRetries | None = None, infer_name: bool = True, toolsets: Sequence[AbstractToolset[AgentDepsT]] | None = None, event_stream_handler: EventStreamHandler[AgentDepsT] | None = None, capabilities: Sequence[AgentCapability[AgentDepsT]] | None = None, spec: dict[str, Any] | AgentSpec | None = None, ) -> result.StreamedRunResultSync[AgentDepsT, OutputDataT] def run_stream_sync( user_prompt: str | Sequence[_messages.UserContent] | None = None, output_type: OutputSpec[RunOutputDataT], message_history: Sequence[_messages.ModelMessage] | None = None, deferred_tool_results: DeferredToolResults | None = None, conversation_id: str | None = None, model: models.Model | models.KnownModelName | str | None = None, deps: AgentDepsT = None, model_settings: AgentModelSettings[AgentDepsT] | None = None, usage_limits: _usage.UsageLimits | None = None, usage: _usage.RunUsage | None = None, metadata: AgentMetadata[AgentDepsT] | None = None, retries: int | AgentRetries | None = None, infer_name: bool = True, toolsets: Sequence[AbstractToolset[AgentDepsT]] | None = None, event_stream_handler: EventStreamHandler[AgentDepsT] | None = None, capabilities: Sequence[AgentCapability[AgentDepsT]] | None = None, spec: dict[str, Any] | AgentSpec | None = None, ) -> result.StreamedRunResultSync[AgentDepsT, RunOutputDataT] ``` Run the agent with a user prompt in sync streaming mode. This is a convenience method that wraps [`run_stream()`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.AbstractAgent.run_stream) with `loop.run_until_complete(...)`. You therefore can't use this method inside async code or if there's an active event loop. This method builds an internal agent graph (using system prompts, tools and output schemas) and then runs the graph until the model produces output matching the `output_type`, for example text or structured data. At this point, a streaming run result object is yielded from which you can stream the output as it comes in, and -- once this output has completed streaming -- get the complete output, message history, and usage. As this method will consider the first output matching the `output_type` to be the final output, it will stop running the agent graph and will not execute any tool calls made by the model after this "final" output. If you want to always run the agent graph to completion and stream events and output at the same time, use [`agent.run()`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.AbstractAgent.run) with an `event_stream_handler` or [`agent.iter()`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.AbstractAgent.iter) instead. Example: ```python from pydantic_ai import Agent agent = Agent('openai:gpt-5.2') def main(): response = agent.run_stream_sync('What is the capital of the UK?') print(response.get_output()) #> The capital of the UK is London. ``` ###### Returns [`result.StreamedRunResultSync`](/docs/ai/api/pydantic-ai/result/#pydantic_ai.result.StreamedRunResultSync)\[`AgentDepsT`, [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] -- The result of the run. ###### Parameters **`user_prompt`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[`_messages.UserContent`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` User input to start/continue the conversation. **`output_type`** : `OutputSpec`\[`RunOutputDataT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Custom output type to use for this run, `output_type` may only be used if the agent has no output validators since output validators would expect an argument that matches the agent's output type. **`message_history`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[`_messages.ModelMessage`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` History of the conversation so far. **`deferred_tool_results`** : [`DeferredToolResults`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.DeferredToolResults) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional results for deferred tool calls in the message history. **`conversation_id`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` ID of the conversation this run belongs to. Pass `'new'` to start a fresh conversation, ignoring any `conversation_id` already on `message_history`. If omitted, falls back to the most recent `conversation_id` on `message_history` or a freshly generated UUID7. **`model`** : [`models.Model`](/docs/ai/api/models/base/#pydantic_ai.models.Model) | [`models.KnownModelName`](/docs/ai/api/models/base/#pydantic_ai.models.KnownModelName) | [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional model to use for this run, required if `model` was not set when creating the agent. **`deps`** : `AgentDepsT` _Default:_ `None` Optional dependencies to use for this run. **`model_settings`** : `AgentModelSettings`\[`AgentDepsT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional settings to use for this model's request, or a callable that receives [`RunContext`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.RunContext) and returns settings. Callables are called before each model request, allowing dynamic per-step settings. **`usage_limits`** : `_usage.UsageLimits` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional limits on model request count or token usage. **`usage`** : `_usage.RunUsage` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional usage to start with, useful for resuming a conversation or agents used in tools. **`metadata`** : `AgentMetadata`\[`AgentDepsT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional metadata to attach to this run. Accepts a dictionary or a callable taking [`RunContext`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.RunContext); merged with the agent's configured metadata. **`retries`** : [`int`](https://docs.python.org/3/library/functions.html#int) | [`AgentRetries`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.AgentRetries) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Override the agent-level retry budgets for this run. Pass an `int` to override the output-validation budget (`AgentRetries(output=...)` equivalent), or an [`AgentRetries`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.AgentRetries) dict for finer control. Tool retries cannot be overridden per run. See [`Agent.__init__`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.Agent.__init__) for semantics of the two enforcement paths. **`infer_name`** : [`bool`](https://docs.python.org/3/library/functions.html#bool) _Default:_ `True` Whether to try to infer the agent name from the call frame if it's not set. **`toolsets`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`AbstractToolset`](/docs/ai/api/pydantic-ai/toolsets/#pydantic_ai.toolsets.AbstractToolset)\[`AgentDepsT`\]\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional additional toolsets for this run. **`event_stream_handler`** : `EventStreamHandler`\[`AgentDepsT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional handler for events from the model's streaming response and the agent's execution of tools to use for this run. It will receive all the events up until the final result is found, which you can then read or stream from inside the context manager. Note that it does _not_ receive any events after the final result is found. **`capabilities`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`AgentCapability`](/docs/ai/api/pydantic-ai/capabilities/#pydantic_ai.capabilities.AgentCapability)\[`AgentDepsT`\]\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional additional [capabilities](https://ai.pydantic.dev/capabilities/) for this run, merged with the agent's configured capabilities. **`spec`** : [`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] | `AgentSpec` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional agent spec to apply for this run. At run time, spec values are additive. ##### run\_stream\_events ```python def run_stream_events( user_prompt: str | Sequence[_messages.UserContent] | None = None, output_type: None = None, message_history: Sequence[_messages.ModelMessage] | None = None, deferred_tool_results: DeferredToolResults | None = None, conversation_id: str | None = None, model: models.Model | models.KnownModelName | str | None = None, instructions: _instructions.AgentInstructions[AgentDepsT] = None, deps: AgentDepsT = None, model_settings: AgentModelSettings[AgentDepsT] | None = None, usage_limits: _usage.UsageLimits | None = None, usage: _usage.RunUsage | None = None, metadata: AgentMetadata[AgentDepsT] | None = None, retries: int | AgentRetries | None = None, infer_name: bool = True, toolsets: Sequence[AbstractToolset[AgentDepsT]] | None = None, capabilities: Sequence[AgentCapability[AgentDepsT]] | None = None, spec: dict[str, Any] | AgentSpec | None = None, ) -> AgentEventStream[OutputDataT] def run_stream_events( user_prompt: str | Sequence[_messages.UserContent] | None = None, output_type: OutputSpec[RunOutputDataT], message_history: Sequence[_messages.ModelMessage] | None = None, deferred_tool_results: DeferredToolResults | None = None, conversation_id: str | None = None, model: models.Model | models.KnownModelName | str | None = None, instructions: _instructions.AgentInstructions[AgentDepsT] = None, deps: AgentDepsT = None, model_settings: AgentModelSettings[AgentDepsT] | None = None, usage_limits: _usage.UsageLimits | None = None, usage: _usage.RunUsage | None = None, metadata: AgentMetadata[AgentDepsT] | None = None, retries: int | AgentRetries | None = None, infer_name: bool = True, toolsets: Sequence[AbstractToolset[AgentDepsT]] | None = None, capabilities: Sequence[AgentCapability[AgentDepsT]] | None = None, spec: dict[str, Any] | AgentSpec | None = None, ) -> AgentEventStream[RunOutputDataT] ``` Run the agent with a user prompt in async mode and stream events from the run. This is a convenience method that wraps [`self.run`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.AbstractAgent.run) and uses the `event_stream_handler` kwarg to get a stream of events from the run. Example: ```python from pydantic_ai import Agent, AgentRunResultEvent, AgentStreamEvent agent = Agent('openai:gpt-5.2') async def main(): events: list[AgentStreamEvent | AgentRunResultEvent] = [] async with agent.run_stream_events('What is the capital of France?') as stream: async for event in stream: events.append(event) print(events) ''' [ PartStartEvent(index=0, part=TextPart(content='The capital of ')), FinalResultEvent(tool_name=None, tool_call_id=None), PartDeltaEvent(index=0, delta=TextPartDelta(content_delta='France is Paris. ')), PartEndEvent( index=0, part=TextPart(content='The capital of France is Paris. ') ), AgentRunResultEvent( result=AgentRunResult(output='The capital of France is Paris. ') ), ] ''' ``` Arguments are the same as for [`self.run`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.AbstractAgent.run), except that `event_stream_handler` is now allowed. ###### Returns `AgentEventStream`\[[`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] -- An `AgentEventStream` async context manager yielding stream events `AgentStreamEvent` and finally a `AgentEventStream`\[[`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] -- `AgentRunResultEvent` with the final run result. ###### Parameters **`user_prompt`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[`_messages.UserContent`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` User input to start/continue the conversation. **`output_type`** : `OutputSpec`\[`RunOutputDataT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Custom output type to use for this run, `output_type` may only be used if the agent has no output validators since output validators would expect an argument that matches the agent's output type. **`message_history`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[`_messages.ModelMessage`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` History of the conversation so far. **`deferred_tool_results`** : [`DeferredToolResults`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.DeferredToolResults) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional results for deferred tool calls in the message history. **`conversation_id`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` ID of the conversation this run belongs to. Pass `'new'` to start a fresh conversation, ignoring any `conversation_id` already on `message_history`. If omitted, falls back to the most recent `conversation_id` on `message_history` or a freshly generated UUID7. **`model`** : [`models.Model`](/docs/ai/api/models/base/#pydantic_ai.models.Model) | [`models.KnownModelName`](/docs/ai/api/models/base/#pydantic_ai.models.KnownModelName) | [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional model to use for this run, required if `model` was not set when creating the agent. **`instructions`** : `_instructions.AgentInstructions`\[`AgentDepsT`\] _Default:_ `None` Optional additional instructions to use for this run. **`deps`** : `AgentDepsT` _Default:_ `None` Optional dependencies to use for this run. **`model_settings`** : `AgentModelSettings`\[`AgentDepsT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional settings to use for this model's request, or a callable that receives [`RunContext`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.RunContext) and returns settings. Callables are called before each model request, allowing dynamic per-step settings. **`usage_limits`** : `_usage.UsageLimits` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional limits on model request count or token usage. **`usage`** : `_usage.RunUsage` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional usage to start with, useful for resuming a conversation or agents used in tools. **`metadata`** : `AgentMetadata`\[`AgentDepsT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional metadata to attach to this run. Accepts a dictionary or a callable taking [`RunContext`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.RunContext); merged with the agent's configured metadata. **`retries`** : [`int`](https://docs.python.org/3/library/functions.html#int) | [`AgentRetries`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.AgentRetries) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Override the agent-level retry budgets for this run. Pass an `int` to override the output-validation budget (`AgentRetries(output=...)` equivalent), or an [`AgentRetries`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.AgentRetries) dict for finer control. Tool retries cannot be overridden per run. See [`Agent.__init__`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.Agent.__init__) for semantics of the two enforcement paths. **`infer_name`** : [`bool`](https://docs.python.org/3/library/functions.html#bool) _Default:_ `True` Whether to try to infer the agent name from the call frame if it's not set. **`toolsets`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`AbstractToolset`](/docs/ai/api/pydantic-ai/toolsets/#pydantic_ai.toolsets.AbstractToolset)\[`AgentDepsT`\]\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional additional toolsets for this run. **`capabilities`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`AgentCapability`](/docs/ai/api/pydantic-ai/capabilities/#pydantic_ai.capabilities.AgentCapability)\[`AgentDepsT`\]\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional additional [capabilities](https://ai.pydantic.dev/capabilities/) for this run, merged with the agent's configured capabilities. **`spec`** : [`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] | `AgentSpec` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional agent spec to apply for this run. At run time, spec values are additive. ##### iter `@abstractmethod` `@async` ```python def iter( user_prompt: str | Sequence[_messages.UserContent] | None = None, output_type: None = None, message_history: Sequence[_messages.ModelMessage] | None = None, deferred_tool_results: DeferredToolResults | None = None, conversation_id: str | None = None, model: models.Model | models.KnownModelName | str | None = None, instructions: _instructions.AgentInstructions[AgentDepsT] = None, deps: AgentDepsT = None, model_settings: AgentModelSettings[AgentDepsT] | None = None, usage_limits: _usage.UsageLimits | None = None, usage: _usage.RunUsage | None = None, metadata: AgentMetadata[AgentDepsT] | None = None, retries: int | AgentRetries | None = None, infer_name: bool = True, toolsets: Sequence[AbstractToolset[AgentDepsT]] | None = None, capabilities: Sequence[AgentCapability[AgentDepsT]] | None = None, spec: dict[str, Any] | AgentSpec | None = None, ) -> AbstractAsyncContextManager[AgentRun[AgentDepsT, OutputDataT]] def iter( user_prompt: str | Sequence[_messages.UserContent] | None = None, output_type: OutputSpec[RunOutputDataT], message_history: Sequence[_messages.ModelMessage] | None = None, deferred_tool_results: DeferredToolResults | None = None, conversation_id: str | None = None, model: models.Model | models.KnownModelName | str | None = None, instructions: _instructions.AgentInstructions[AgentDepsT] = None, deps: AgentDepsT = None, model_settings: AgentModelSettings[AgentDepsT] | None = None, usage_limits: _usage.UsageLimits | None = None, usage: _usage.RunUsage | None = None, metadata: AgentMetadata[AgentDepsT] | None = None, retries: int | AgentRetries | None = None, infer_name: bool = True, toolsets: Sequence[AbstractToolset[AgentDepsT]] | None = None, capabilities: Sequence[AgentCapability[AgentDepsT]] | None = None, spec: dict[str, Any] | AgentSpec | None = None, ) -> AbstractAsyncContextManager[AgentRun[AgentDepsT, RunOutputDataT]] ``` A contextmanager which can be used to iterate over the agent graph's nodes as they are executed. This method builds an internal agent graph (using system prompts, tools and output schemas) and then returns an `AgentRun` object. The `AgentRun` can be used to async-iterate over the nodes of the graph as they are executed. This is the API to use if you want to consume the outputs coming from each LLM model response, or the stream of events coming from the execution of tools. The `AgentRun` also provides methods to access the full message history, new messages, and usage statistics, and the final result of the run once it has completed. For more details, see the documentation of `AgentRun`. Example: ```python from pydantic_ai import Agent agent = Agent('openai:gpt-5.2') async def main(): nodes = [] async with agent.iter('What is the capital of France?') as agent_run: async for node in agent_run: nodes.append(node) print(nodes) ''' [ UserPromptNode( user_prompt='What is the capital of France?', instructions_functions=[], system_prompts=(), system_prompt_functions=[], system_prompt_dynamic_functions={}, ), ModelRequestNode( request=ModelRequest( parts=[ UserPromptPart( content='What is the capital of France?', timestamp=datetime.datetime(...), ) ], timestamp=datetime.datetime(...), run_id='...', conversation_id='...', ) ), CallToolsNode( model_response=ModelResponse( parts=[TextPart(content='The capital of France is Paris.')], usage=RequestUsage(input_tokens=56, output_tokens=7), model_name='gpt-5.2', timestamp=datetime.datetime(...), run_id='...', conversation_id='...', ) ), End(data=FinalResult(output='The capital of France is Paris.')), ] ''' print(agent_run.result.output) #> The capital of France is Paris. ``` ###### Returns [`AsyncIterator`](https://docs.python.org/3/library/typing.html#typing.AsyncIterator)\[[`AgentRun`](/docs/ai/api/pydantic-ai/run/#pydantic_ai.run.AgentRun)\[`AgentDepsT`, [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\]\] -- The result of the run. ###### Parameters **`user_prompt`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[`_messages.UserContent`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` User input to start/continue the conversation. **`output_type`** : `OutputSpec`\[`RunOutputDataT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Custom output type to use for this run, `output_type` may only be used if the agent has no output validators since output validators would expect an argument that matches the agent's output type. **`message_history`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[`_messages.ModelMessage`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` History of the conversation so far. **`deferred_tool_results`** : [`DeferredToolResults`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.DeferredToolResults) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional results for deferred tool calls in the message history. **`conversation_id`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` ID of the conversation this run belongs to. Pass `'new'` to start a fresh conversation, ignoring any `conversation_id` already on `message_history`. If omitted, falls back to the most recent `conversation_id` on `message_history` or a freshly generated UUID7. **`model`** : [`models.Model`](/docs/ai/api/models/base/#pydantic_ai.models.Model) | [`models.KnownModelName`](/docs/ai/api/models/base/#pydantic_ai.models.KnownModelName) | [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional model to use for this run, required if `model` was not set when creating the agent. **`instructions`** : `_instructions.AgentInstructions`\[`AgentDepsT`\] _Default:_ `None` Optional additional instructions to use for this run. **`deps`** : `AgentDepsT` _Default:_ `None` Optional dependencies to use for this run. **`model_settings`** : `AgentModelSettings`\[`AgentDepsT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional settings to use for this model's request, or a callable that receives [`RunContext`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.RunContext) and returns settings. Callables are called before each model request, allowing dynamic per-step settings. **`usage_limits`** : `_usage.UsageLimits` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional limits on model request count or token usage. **`usage`** : `_usage.RunUsage` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional usage to start with, useful for resuming a conversation or agents used in tools. **`metadata`** : `AgentMetadata`\[`AgentDepsT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional metadata to attach to this run. Accepts a dictionary or a callable taking [`RunContext`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.RunContext); merged with the agent's configured metadata. **`retries`** : [`int`](https://docs.python.org/3/library/functions.html#int) | [`AgentRetries`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.AgentRetries) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Override the agent-level retry budgets for this run. Pass an `int` to override the output-validation budget (`AgentRetries(output=...)` equivalent), or an [`AgentRetries`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.AgentRetries) dict for finer control. Tool retries cannot be overridden per run. See [`Agent.__init__`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.Agent.__init__) for semantics of the two enforcement paths. **`infer_name`** : [`bool`](https://docs.python.org/3/library/functions.html#bool) _Default:_ `True` Whether to try to infer the agent name from the call frame if it's not set. **`toolsets`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`AbstractToolset`](/docs/ai/api/pydantic-ai/toolsets/#pydantic_ai.toolsets.AbstractToolset)\[`AgentDepsT`\]\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional additional toolsets for this run. **`capabilities`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`AgentCapability`](/docs/ai/api/pydantic-ai/capabilities/#pydantic_ai.capabilities.AgentCapability)\[`AgentDepsT`\]\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional additional [capabilities](https://ai.pydantic.dev/capabilities/) for this run, merged with the agent's configured capabilities. **`spec`** : [`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] | `AgentSpec` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional agent spec to apply for this run. At run time, spec values are additive. ##### override `@abstractmethod` ```python def override( name: str | _utils.Unset = _utils.UNSET, deps: AgentDepsT | _utils.Unset = _utils.UNSET, model: models.Model | models.KnownModelName | str | _utils.Unset = _utils.UNSET, toolsets: Sequence[AbstractToolset[AgentDepsT]] | _utils.Unset = _utils.UNSET, tools: Sequence[Tool[AgentDepsT] | ToolFuncEither[AgentDepsT, ...]] | _utils.Unset = _utils.UNSET, native_tools: Sequence[AgentNativeTool[AgentDepsT]] | _utils.Unset = _utils.UNSET, instructions: _instructions.AgentInstructions[AgentDepsT] | _utils.Unset = _utils.UNSET, model_settings: AgentModelSettings[AgentDepsT] | _utils.Unset = _utils.UNSET, retries: int | AgentRetries | _utils.Unset = _utils.UNSET, spec: dict[str, Any] | AgentSpec | None = None, ) -> Iterator[None] ``` Context manager to temporarily override agent configuration. This is particularly useful when testing. You can find an example of this [here](/docs/ai/guides/testing#overriding-model-via-pytest-fixtures). ###### Returns [`Iterator`](https://docs.python.org/3/library/typing.html#typing.Iterator)\[[`None`](https://docs.python.org/3/library/constants.html#None)\] ###### Parameters **`name`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | `_utils.Unset` _Default:_ `_utils.UNSET` The name to use instead of the name passed to the agent constructor and agent run. **`deps`** : `AgentDepsT` | `_utils.Unset` _Default:_ `_utils.UNSET` The dependencies to use instead of the dependencies passed to the agent run. **`model`** : [`models.Model`](/docs/ai/api/models/base/#pydantic_ai.models.Model) | [`models.KnownModelName`](/docs/ai/api/models/base/#pydantic_ai.models.KnownModelName) | [`str`](https://docs.python.org/3/library/stdtypes.html#str) | `_utils.Unset` _Default:_ `_utils.UNSET` The model to use instead of the model passed to the agent run. **`toolsets`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`AbstractToolset`](/docs/ai/api/pydantic-ai/toolsets/#pydantic_ai.toolsets.AbstractToolset)\[`AgentDepsT`\]\] | `_utils.Unset` _Default:_ `_utils.UNSET` The toolsets to use instead of the toolsets passed to the agent constructor and agent run. **`tools`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`Tool`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.Tool)\[`AgentDepsT`\] | `ToolFuncEither`\[`AgentDepsT`, ...\]\] | `_utils.Unset` _Default:_ `_utils.UNSET` The tools to use instead of the tools registered with the agent. **`native_tools`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`AgentNativeTool`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.AgentNativeTool)\[`AgentDepsT`\]\] | `_utils.Unset` _Default:_ `_utils.UNSET` The native tools to use instead of the agent's configured native tools. **`instructions`** : `_instructions.AgentInstructions`\[`AgentDepsT`\] | `_utils.Unset` _Default:_ `_utils.UNSET` The instructions to use instead of the instructions registered with the agent. **`model_settings`** : `AgentModelSettings`\[`AgentDepsT`\] | `_utils.Unset` _Default:_ `_utils.UNSET` The model settings to use instead of the model settings passed to the agent constructor. When set, any per-run `model_settings` argument is ignored. **`retries`** : [`int`](https://docs.python.org/3/library/functions.html#int) | [`AgentRetries`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.AgentRetries) | `_utils.Unset` _Default:_ `_utils.UNSET` The retry budgets to use instead of the agent-level configuration. Pass an `int` to override the output-validation budget, or an [`AgentRetries`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.AgentRetries) dict for finer control. When set, any per-run `retries` argument is ignored. Tool retries cannot be overridden via `override()`. **`spec`** : [`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] | `AgentSpec` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional agent spec providing defaults for override. ##### parallel\_tool\_call\_execution\_mode `@staticmethod` ```python def parallel_tool_call_execution_mode( mode: tool_manager.ParallelExecutionMode = 'parallel', ) -> Iterator[None] ``` Set the parallel execution mode during the context. ###### Returns [`Iterator`](https://docs.python.org/3/library/typing.html#typing.Iterator)\[[`None`](https://docs.python.org/3/library/constants.html#None)\] ###### Parameters **`mode`** : `tool_manager.ParallelExecutionMode` _Default:_ `'parallel'` The execution mode for tool calls: - 'parallel': Run tool calls in parallel, yielding events as they complete (default). - 'sequential': Run tool calls one at a time in order. - 'parallel\_ordered\_events': Run tool calls in parallel, but events are emitted in order, after all calls complete. ##### sequential\_tool\_calls `@deprecated` `@staticmethod` ```python def sequential_tool_calls() -> Iterator[None] ``` Run tool calls sequentially during the context. ###### Returns [`Iterator`](https://docs.python.org/3/library/typing.html#typing.Iterator)\[[`None`](https://docs.python.org/3/library/constants.html#None)\] ##### using\_thread\_executor `@staticmethod` ```python def using_thread_executor(executor: Executor) -> Iterator[None] ``` Use a custom executor for running sync functions in threads during the context. By default, sync tool functions and other sync callbacks are run in threads using `anyio.to_thread.run_sync`, which creates ephemeral threads. In long-running servers (e.g. FastAPI), this can lead to thread accumulation under sustained load. This context manager lets you provide a bounded [`ThreadPoolExecutor`](https://docs.python.org/3/library/concurrent.futures.html#concurrent.futures.ThreadPoolExecutor) (or any [`Executor`](https://docs.python.org/3/library/concurrent.futures.html#concurrent.futures.Executor)) to control thread lifecycle: ```python from concurrent.futures import ThreadPoolExecutor from contextlib import asynccontextmanager from pydantic_ai import Agent @asynccontextmanager async def lifespan(app): executor = ThreadPoolExecutor(max_workers=16) with Agent.using_thread_executor(executor): yield executor.shutdown(wait=True) ``` For per-agent configuration, use the [`ThreadExecutor`](/docs/ai/api/pydantic-ai/capabilities/#pydantic_ai.capabilities.ThreadExecutor) capability instead. ###### Returns [`Iterator`](https://docs.python.org/3/library/typing.html#typing.Iterator)\[[`None`](https://docs.python.org/3/library/constants.html#None)\] ###### Parameters **`executor`** : `Executor` The executor to use for running sync functions. ##### is\_model\_request\_node `@staticmethod` ```python def is_model_request_node( node: _agent_graph.AgentNode[T, S] | End[result.FinalResult[S]], ) -> TypeIs[_agent_graph.ModelRequestNode[T, S]] ``` Check if the node is a `ModelRequestNode`, narrowing the type if it is. This method preserves the generic parameters while narrowing the type, unlike a direct call to `isinstance`. ###### Returns [`TypeIs`](https://docs.python.org/3/library/typing.html#typing.TypeIs)\[`_agent_graph.ModelRequestNode`\[`T`, `S`\]\] ##### is\_call\_tools\_node `@staticmethod` ```python def is_call_tools_node( node: _agent_graph.AgentNode[T, S] | End[result.FinalResult[S]], ) -> TypeIs[_agent_graph.CallToolsNode[T, S]] ``` Check if the node is a `CallToolsNode`, narrowing the type if it is. This method preserves the generic parameters while narrowing the type, unlike a direct call to `isinstance`. ###### Returns [`TypeIs`](https://docs.python.org/3/library/typing.html#typing.TypeIs)\[`_agent_graph.CallToolsNode`\[`T`, `S`\]\] ##### is\_user\_prompt\_node `@staticmethod` ```python def is_user_prompt_node( node: _agent_graph.AgentNode[T, S] | End[result.FinalResult[S]], ) -> TypeIs[_agent_graph.UserPromptNode[T, S]] ``` Check if the node is a `UserPromptNode`, narrowing the type if it is. This method preserves the generic parameters while narrowing the type, unlike a direct call to `isinstance`. ###### Returns [`TypeIs`](https://docs.python.org/3/library/typing.html#typing.TypeIs)\[`_agent_graph.UserPromptNode`\[`T`, `S`\]\] ##### is\_end\_node `@staticmethod` ```python def is_end_node( node: _agent_graph.AgentNode[T, S] | End[result.FinalResult[S]], ) -> TypeIs[End[result.FinalResult[S]]] ``` Check if the node is a `End`, narrowing the type if it is. This method preserves the generic parameters while narrowing the type, unlike a direct call to `isinstance`. ###### Returns [`TypeIs`](https://docs.python.org/3/library/typing.html#typing.TypeIs)\[`End`\[`result.FinalResult`\[`S`\]\]\] ##### to\_ag\_ui `@deprecated` ```python def to_ag_ui( output_type: OutputSpec[OutputDataT] | None = None, message_history: Sequence[_messages.ModelMessage] | None = None, deferred_tool_results: DeferredToolResults | None = None, conversation_id: str | None = None, model: models.Model | models.KnownModelName | str | None = None, deps: AgentDepsT = None, model_settings: ModelSettings | None = None, usage_limits: UsageLimits | None = None, usage: RunUsage | None = None, infer_name: bool = True, toolsets: Sequence[AbstractToolset[AgentDepsT]] | None = None, debug: bool = False, routes: Sequence[BaseRoute] | None = None, middleware: Sequence[Middleware] | None = None, exception_handlers: Mapping[Any, ExceptionHandler] | None = None, on_startup: Sequence[Callable[[], Any]] | None = None, on_shutdown: Sequence[Callable[[], Any]] | None = None, lifespan: Lifespan[AGUIApp[AgentDepsT, OutputDataT]] | None = None, ) -> AGUIApp[AgentDepsT, OutputDataT] ``` Returns an ASGI application that handles every AG-UI request by running the agent. Note that the `deps` will be the same for each request, with the exception of the AG-UI state that's injected into the `state` field of a `deps` object that implements the [`StateHandler`](/docs/ai/api/ui/base/#pydantic_ai.ui.StateHandler) protocol. To provide different `deps` for each request (e.g. based on the authenticated user), compose [`AGUIAdapter`](/docs/ai/api/ui/ag_ui/#pydantic_ai.ui.ag_ui.AGUIAdapter) directly via `AGUIAdapter.dispatch_request()` instead. Example: ```python from pydantic_ai import Agent agent = Agent('openai:gpt-5.2') app = agent.to_ag_ui() ``` The `app` is an ASGI application that can be used with any ASGI server. To run the application, you can use the following command: Terminal ```bash uvicorn app:app --host 0.0.0.0 --port 8000 ``` See [AG-UI docs](/docs/ai/integrations/ui/ag-ui) for more information. ###### Returns `AGUIApp`\[`AgentDepsT`, `OutputDataT`\] -- An ASGI application for running Pydantic AI agents with AG-UI protocol support. ###### Parameters **`output_type`** : `OutputSpec`\[`OutputDataT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Custom output type to use for this run, `output_type` may only be used if the agent has no output validators since output validators would expect an argument that matches the agent's output type. **`message_history`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[`_messages.ModelMessage`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` History of the conversation so far. **`deferred_tool_results`** : [`DeferredToolResults`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.DeferredToolResults) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional results for deferred tool calls in the message history. **`conversation_id`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` ID of the conversation this run belongs to. Pass `'new'` to start a fresh conversation, ignoring any `conversation_id` already on `message_history`. If omitted, falls back to the most recent `conversation_id` on `message_history` or a freshly generated UUID7. **`model`** : [`models.Model`](/docs/ai/api/models/base/#pydantic_ai.models.Model) | [`models.KnownModelName`](/docs/ai/api/models/base/#pydantic_ai.models.KnownModelName) | [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional model to use for this run, required if `model` was not set when creating the agent. **`deps`** : `AgentDepsT` _Default:_ `None` Optional dependencies to use for this run. **`model_settings`** : [`ModelSettings`](/docs/ai/api/pydantic-ai/settings/#pydantic_ai.settings.ModelSettings) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional settings to use for this model's request. **`usage_limits`** : [`UsageLimits`](/docs/ai/api/pydantic-ai/usage/#pydantic_ai.usage.UsageLimits) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional limits on model request count or token usage. **`usage`** : [`RunUsage`](/docs/ai/api/pydantic-ai/usage/#pydantic_ai.usage.RunUsage) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional usage to start with, useful for resuming a conversation or agents used in tools. **`infer_name`** : [`bool`](https://docs.python.org/3/library/functions.html#bool) _Default:_ `True` Whether to try to infer the agent name from the call frame if it's not set. **`toolsets`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`AbstractToolset`](/docs/ai/api/pydantic-ai/toolsets/#pydantic_ai.toolsets.AbstractToolset)\[`AgentDepsT`\]\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional additional toolsets for this run. **`debug`** : [`bool`](https://docs.python.org/3/library/functions.html#bool) _Default:_ `False` Boolean indicating if debug tracebacks should be returned on errors. **`routes`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[`BaseRoute`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` A list of routes to serve incoming HTTP and WebSocket requests. **`middleware`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[`Middleware`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` A list of middleware to run for every request. A starlette application will always automatically include two middleware classes. `ServerErrorMiddleware` is added as the very outermost middleware, to handle any uncaught errors occurring anywhere in the entire stack. `ExceptionMiddleware` is added as the very innermost middleware, to deal with handled exception cases occurring in the routing or endpoints. **`exception_handlers`** : [`Mapping`](https://docs.python.org/3/library/typing.html#typing.Mapping)\[[`Any`](https://docs.python.org/3/library/typing.html#typing.Any), `ExceptionHandler`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` A mapping of either integer status codes, or exception class types onto callables which handle the exceptions. Exception handler callables should be of the form `handler(request, exc) -> response` and may be either standard functions, or async functions. **`on_startup`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`Callable`](https://docs.python.org/3/library/typing.html#typing.Callable)\[\[\], [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\]\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` A list of callables to run on application startup. Startup handler callables do not take any arguments, and may be either standard functions, or async functions. **`on_shutdown`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`Callable`](https://docs.python.org/3/library/typing.html#typing.Callable)\[\[\], [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\]\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` A list of callables to run on application shutdown. Shutdown handler callables do not take any arguments, and may be either standard functions, or async functions. **`lifespan`** : `Lifespan`\[`AGUIApp`\[`AgentDepsT`, `OutputDataT`\]\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` A lifespan context function, which can be used to perform startup and shutdown tasks. This is a newer style that replaces the `on_startup` and `on_shutdown` handlers. Use one or the other, not both. ##### to\_a2a `@deprecated` ```python def to_a2a( storage: Storage | None = None, broker: Broker | None = None, name: str | None = None, url: str = 'http://localhost:8000', version: str = '1.0.0', description: str | None = None, provider: AgentProvider | None = None, skills: list[Skill] | None = None, debug: bool = False, routes: Sequence[Route] | None = None, middleware: Sequence[Middleware] | None = None, exception_handlers: dict[Any, ExceptionHandler] | None = None, lifespan: Lifespan[FastA2A] | None = None, ) -> FastA2A ``` Convert the agent to a FastA2A application. Deprecated in 1.x and removed in 2.0; use `agent_to_a2a` from [`fasta2a`](https://github.com/datalayer/fasta2a) (v0.6.1+) instead: ```python from fasta2a.pydantic_ai import agent_to_a2a from pydantic_ai import Agent agent = Agent('openai:gpt-5.2') app = agent_to_a2a(agent) ``` The `app` is an ASGI application that can be used with any ASGI server. To run the application, you can use the following command: Terminal ```bash uvicorn app:app --host 0.0.0.0 --port 8000 ``` ###### Returns `FastA2A` ##### to\_cli `@async` ```python def to_cli( deps: AgentDepsT = None, prog_name: str = 'pydantic-ai', message_history: Sequence[_messages.ModelMessage] | None = None, model_settings: ModelSettings | None = None, usage_limits: _usage.UsageLimits | None = None, ) -> None ``` Run the agent in a CLI chat interface. Example: agent\_to\_cli.py ```python from pydantic_ai import Agent agent = Agent('openai:gpt-5.2', instructions='You always respond in Italian.') async def main(): await agent.to_cli() ``` ###### Returns [`None`](https://docs.python.org/3/library/constants.html#None) ###### Parameters **`deps`** : `AgentDepsT` _Default:_ `None` The dependencies to pass to the agent. **`prog_name`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) _Default:_ `'pydantic-ai'` The name of the program to use for the CLI. Defaults to 'pydantic-ai'. **`message_history`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[`_messages.ModelMessage`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` History of the conversation so far. **`model_settings`** : [`ModelSettings`](/docs/ai/api/pydantic-ai/settings/#pydantic_ai.settings.ModelSettings) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional settings to use for this model's request. **`usage_limits`** : `_usage.UsageLimits` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional limits on model request count or token usage. ##### to\_cli\_sync ```python def to_cli_sync( deps: AgentDepsT = None, prog_name: str = 'pydantic-ai', message_history: Sequence[_messages.ModelMessage] | None = None, model_settings: ModelSettings | None = None, usage_limits: _usage.UsageLimits | None = None, ) -> None ``` Run the agent in a CLI chat interface with the non-async interface. agent\_to\_cli\_sync.py ```python from pydantic_ai import Agent agent = Agent('openai:gpt-5.2', instructions='You always respond in Italian.') agent.to_cli_sync() agent.to_cli_sync(prog_name='assistant') ``` ###### Returns [`None`](https://docs.python.org/3/library/constants.html#None) ###### Parameters **`deps`** : `AgentDepsT` _Default:_ `None` The dependencies to pass to the agent. **`prog_name`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) _Default:_ `'pydantic-ai'` The name of the program to use for the CLI. Defaults to 'pydantic-ai'. **`message_history`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[`_messages.ModelMessage`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` History of the conversation so far. **`model_settings`** : [`ModelSettings`](/docs/ai/api/pydantic-ai/settings/#pydantic_ai.settings.ModelSettings) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional settings to use for this model's request. **`usage_limits`** : `_usage.UsageLimits` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional limits on model request count or token usage. ### WrapperAgent **Bases:** `AbstractAgent[AgentDepsT, OutputDataT]` Agent which wraps another agent. Does nothing on its own, used as a base class. #### Methods ##### iter `@async` ```python def iter( user_prompt: str | Sequence[_messages.UserContent] | None = None, output_type: None = None, message_history: Sequence[_messages.ModelMessage] | None = None, deferred_tool_results: DeferredToolResults | None = None, conversation_id: str | None = None, model: models.Model | models.KnownModelName | str | None = None, instructions: _instructions.AgentInstructions[AgentDepsT] = None, deps: AgentDepsT = None, model_settings: AgentModelSettings[AgentDepsT] | None = None, usage_limits: _usage.UsageLimits | None = None, usage: _usage.RunUsage | None = None, metadata: AgentMetadata[AgentDepsT] | None = None, retries: int | AgentRetries | None = None, infer_name: bool = True, toolsets: Sequence[AbstractToolset[AgentDepsT]] | None = None, capabilities: Sequence[AgentCapability[AgentDepsT]] | None = None, spec: dict[str, Any] | AgentSpec | None = None, ) -> AbstractAsyncContextManager[AgentRun[AgentDepsT, OutputDataT]] def iter( user_prompt: str | Sequence[_messages.UserContent] | None = None, output_type: OutputSpec[RunOutputDataT], message_history: Sequence[_messages.ModelMessage] | None = None, deferred_tool_results: DeferredToolResults | None = None, conversation_id: str | None = None, model: models.Model | models.KnownModelName | str | None = None, instructions: _instructions.AgentInstructions[AgentDepsT] = None, deps: AgentDepsT = None, model_settings: AgentModelSettings[AgentDepsT] | None = None, usage_limits: _usage.UsageLimits | None = None, usage: _usage.RunUsage | None = None, metadata: AgentMetadata[AgentDepsT] | None = None, retries: int | AgentRetries | None = None, infer_name: bool = True, toolsets: Sequence[AbstractToolset[AgentDepsT]] | None = None, capabilities: Sequence[AgentCapability[AgentDepsT]] | None = None, spec: dict[str, Any] | AgentSpec | None = None, ) -> AbstractAsyncContextManager[AgentRun[AgentDepsT, RunOutputDataT]] ``` A contextmanager which can be used to iterate over the agent graph's nodes as they are executed. This method builds an internal agent graph (using system prompts, tools and output schemas) and then returns an `AgentRun` object. The `AgentRun` can be used to async-iterate over the nodes of the graph as they are executed. This is the API to use if you want to consume the outputs coming from each LLM model response, or the stream of events coming from the execution of tools. The `AgentRun` also provides methods to access the full message history, new messages, and usage statistics, and the final result of the run once it has completed. For more details, see the documentation of `AgentRun`. Example: ```python from pydantic_ai import Agent agent = Agent('openai:gpt-5.2') async def main(): nodes = [] async with agent.iter('What is the capital of France?') as agent_run: async for node in agent_run: nodes.append(node) print(nodes) ''' [ UserPromptNode( user_prompt='What is the capital of France?', instructions_functions=[], system_prompts=(), system_prompt_functions=[], system_prompt_dynamic_functions={}, ), ModelRequestNode( request=ModelRequest( parts=[ UserPromptPart( content='What is the capital of France?', timestamp=datetime.datetime(...), ) ], timestamp=datetime.datetime(...), run_id='...', conversation_id='...', ) ), CallToolsNode( model_response=ModelResponse( parts=[TextPart(content='The capital of France is Paris.')], usage=RequestUsage(input_tokens=56, output_tokens=7), model_name='gpt-5.2', timestamp=datetime.datetime(...), run_id='...', conversation_id='...', ) ), End(data=FinalResult(output='The capital of France is Paris.')), ] ''' print(agent_run.result.output) #> The capital of France is Paris. ``` ###### Returns [`AsyncIterator`](https://docs.python.org/3/library/typing.html#typing.AsyncIterator)\[[`AgentRun`](/docs/ai/api/pydantic-ai/run/#pydantic_ai.run.AgentRun)\[`AgentDepsT`, [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\]\] -- The result of the run. ###### Parameters **`user_prompt`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[`_messages.UserContent`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` User input to start/continue the conversation. **`output_type`** : `OutputSpec`\[`RunOutputDataT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Custom output type to use for this run, `output_type` may only be used if the agent has no output validators since output validators would expect an argument that matches the agent's output type. **`message_history`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[`_messages.ModelMessage`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` History of the conversation so far. **`deferred_tool_results`** : [`DeferredToolResults`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.DeferredToolResults) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional results for deferred tool calls in the message history. **`conversation_id`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` ID of the conversation this run belongs to. Pass `'new'` to start a fresh conversation, ignoring any `conversation_id` already on `message_history`. If omitted, falls back to the most recent `conversation_id` on `message_history` or a freshly generated UUID7. **`model`** : [`models.Model`](/docs/ai/api/models/base/#pydantic_ai.models.Model) | [`models.KnownModelName`](/docs/ai/api/models/base/#pydantic_ai.models.KnownModelName) | [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional model to use for this run, required if `model` was not set when creating the agent. **`instructions`** : `_instructions.AgentInstructions`\[`AgentDepsT`\] _Default:_ `None` Optional additional instructions to use for this run. **`deps`** : `AgentDepsT` _Default:_ `None` Optional dependencies to use for this run. **`model_settings`** : `AgentModelSettings`\[`AgentDepsT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional settings to use for this model's request. **`usage_limits`** : `_usage.UsageLimits` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional limits on model request count or token usage. **`usage`** : `_usage.RunUsage` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional usage to start with, useful for resuming a conversation or agents used in tools. **`metadata`** : `AgentMetadata`\[`AgentDepsT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional metadata to attach to this run. **`retries`** : [`int`](https://docs.python.org/3/library/functions.html#int) | [`AgentRetries`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.AgentRetries) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Override the agent-level retry budgets for this run. Pass an `int` to override the output-validation budget (`AgentRetries(output=...)` equivalent), or an [`AgentRetries`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.AgentRetries) dict for finer control. Tool retries cannot be overridden per run. See [`Agent.__init__`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.Agent.__init__) for semantics of the two enforcement paths. **`infer_name`** : [`bool`](https://docs.python.org/3/library/functions.html#bool) _Default:_ `True` Whether to try to infer the agent name from the call frame if it's not set. **`toolsets`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`AbstractToolset`](/docs/ai/api/pydantic-ai/toolsets/#pydantic_ai.toolsets.AbstractToolset)\[`AgentDepsT`\]\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional additional toolsets for this run. **`capabilities`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`AgentCapability`](/docs/ai/api/pydantic-ai/capabilities/#pydantic_ai.capabilities.AgentCapability)\[`AgentDepsT`\]\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional additional [capabilities](https://ai.pydantic.dev/capabilities/) for this run, merged with the agent's configured capabilities. **`spec`** : [`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] | `AgentSpec` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional agent spec to apply for this run. ##### override ```python def override( name: str | _utils.Unset = _utils.UNSET, deps: AgentDepsT | _utils.Unset = _utils.UNSET, model: models.Model | models.KnownModelName | str | _utils.Unset = _utils.UNSET, toolsets: Sequence[AbstractToolset[AgentDepsT]] | _utils.Unset = _utils.UNSET, tools: Sequence[Tool[AgentDepsT] | ToolFuncEither[AgentDepsT, ...]] | _utils.Unset = _utils.UNSET, native_tools: Sequence[AgentNativeTool[AgentDepsT]] | _utils.Unset = _utils.UNSET, instructions: _instructions.AgentInstructions[AgentDepsT] | _utils.Unset = _utils.UNSET, model_settings: AgentModelSettings[AgentDepsT] | _utils.Unset = _utils.UNSET, retries: int | AgentRetries | _utils.Unset = _utils.UNSET, spec: dict[str, Any] | AgentSpec | None = None, _deprecated_kwargs: Any = {}, ) -> Iterator[None] ``` Context manager to temporarily override agent configuration. This is particularly useful when testing. You can find an example of this [here](/docs/ai/guides/testing#overriding-model-via-pytest-fixtures). ###### Returns [`Iterator`](https://docs.python.org/3/library/typing.html#typing.Iterator)\[[`None`](https://docs.python.org/3/library/constants.html#None)\] ###### Parameters **`name`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | `_utils.Unset` _Default:_ `_utils.UNSET` The name to use instead of the name passed to the agent constructor and agent run. **`deps`** : `AgentDepsT` | `_utils.Unset` _Default:_ `_utils.UNSET` The dependencies to use instead of the dependencies passed to the agent run. **`model`** : [`models.Model`](/docs/ai/api/models/base/#pydantic_ai.models.Model) | [`models.KnownModelName`](/docs/ai/api/models/base/#pydantic_ai.models.KnownModelName) | [`str`](https://docs.python.org/3/library/stdtypes.html#str) | `_utils.Unset` _Default:_ `_utils.UNSET` The model to use instead of the model passed to the agent run. **`toolsets`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`AbstractToolset`](/docs/ai/api/pydantic-ai/toolsets/#pydantic_ai.toolsets.AbstractToolset)\[`AgentDepsT`\]\] | `_utils.Unset` _Default:_ `_utils.UNSET` The toolsets to use instead of the toolsets passed to the agent constructor and agent run. **`tools`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`Tool`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.Tool)\[`AgentDepsT`\] | `ToolFuncEither`\[`AgentDepsT`, ...\]\] | `_utils.Unset` _Default:_ `_utils.UNSET` The tools to use instead of the tools registered with the agent. **`native_tools`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`AgentNativeTool`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.AgentNativeTool)\[`AgentDepsT`\]\] | `_utils.Unset` _Default:_ `_utils.UNSET` The native tools to use instead of the agent's configured native tools. **`instructions`** : `_instructions.AgentInstructions`\[`AgentDepsT`\] | `_utils.Unset` _Default:_ `_utils.UNSET` The instructions to use instead of the instructions registered with the agent. **`model_settings`** : `AgentModelSettings`\[`AgentDepsT`\] | `_utils.Unset` _Default:_ `_utils.UNSET` The model settings to use instead of the model settings passed to the agent constructor. When set, any per-run `model_settings` argument is ignored. **`retries`** : [`int`](https://docs.python.org/3/library/functions.html#int) | [`AgentRetries`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.AgentRetries) | `_utils.Unset` _Default:_ `_utils.UNSET` The retry budgets to use instead of the agent-level configuration. Pass an `int` to override the output-validation budget, or an [`AgentRetries`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.AgentRetries) dict for finer control. When set, any per-run `retries` argument is ignored. **`spec`** : [`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] | `AgentSpec` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional agent spec to apply as overrides. ### AgentRetries **Bases:** [`TypedDict`](https://docs.python.org/3/library/typing.html#typing.TypedDict) Per-category retry budgets for an [`Agent`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.Agent). Pass to `Agent(retries=...)` as a dict to set different budgets per category. `int` semantics differ by call site: - At `Agent(retries=N)` construction time, an `int` sets both `tools` and `output` to `N`. - At `run()` / `iter()` / `override()` time, an `int` overrides only the `output` budget. Tool retries cannot be overridden per run or via `override()` -- passing `retries={'tools': ...}` at those call sites raises a `UserError`, since the tool manager is built once at agent construction. ### AgentRun **Bases:** `Generic[AgentDepsT, OutputDataT]` A stateful, async-iterable run of an [`Agent`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.Agent). You generally obtain an `AgentRun` instance by calling `async with my_agent.iter(...) as agent_run:`. Once you have an instance, you can use it to iterate through the run's nodes as they execute. When an `End` is reached, the run finishes and [`result`](/docs/ai/api/pydantic-ai/run/#pydantic_ai.run.AgentRun.result) becomes available. Example: ```python from pydantic_ai import Agent agent = Agent('openai:gpt-5.2') async def main(): nodes = [] # Iterate through the run, recording each node along the way: async with agent.iter('What is the capital of France?') as agent_run: async for node in agent_run: nodes.append(node) print(nodes) ''' [ UserPromptNode( user_prompt='What is the capital of France?', instructions_functions=[], system_prompts=(), system_prompt_functions=[], system_prompt_dynamic_functions={}, ), ModelRequestNode( request=ModelRequest( parts=[ UserPromptPart( content='What is the capital of France?', timestamp=datetime.datetime(...), ) ], timestamp=datetime.datetime(...), run_id='...', conversation_id='...', ) ), CallToolsNode( model_response=ModelResponse( parts=[TextPart(content='The capital of France is Paris.')], usage=RequestUsage(input_tokens=56, output_tokens=7), model_name='gpt-5.2', timestamp=datetime.datetime(...), run_id='...', conversation_id='...', ) ), End(data=FinalResult(output='The capital of France is Paris.')), ] ''' print(agent_run.result.output) #> The capital of France is Paris. ``` You can also manually drive the iteration using the [`next`](/docs/ai/api/pydantic-ai/run/#pydantic_ai.run.AgentRun.next) method for more granular control. #### Attributes ##### ctx The current context of the agent run. **Type:** `GraphRunContext`\[`_agent_graph.GraphAgentState`, `_agent_graph.GraphAgentDeps`\[`AgentDepsT`, [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\]\] ##### next\_node The next node that will be run in the agent graph. This is the next node that will be used during async iteration, or if a node is not passed to `self.next(...)`. **Type:** `_agent_graph.AgentNode`\[`AgentDepsT`, `OutputDataT`\] | `End`\[`FinalResult`\[`OutputDataT`\]\] ##### result The final result of the run if it has ended, otherwise `None`. Once the run returns an `End` node, `result` is populated with an [`AgentRunResult`](/docs/ai/api/pydantic-ai/run/#pydantic_ai.run.AgentRunResult). **Type:** [`AgentRunResult`](/docs/ai/api/pydantic-ai/run/#pydantic_ai.run.AgentRunResult)\[`OutputDataT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) ##### metadata Metadata associated with this agent run, if configured. **Type:** [`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] | [`None`](https://docs.python.org/3/library/constants.html#None) ##### run\_id The unique identifier for the agent run. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### conversation\_id The unique identifier for the conversation this run belongs to. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) #### Methods ##### all\_messages ```python def all_messages() -> list[_messages.ModelMessage] ``` Return all messages for the run so far. Messages from older runs are included. ###### Returns [`list`](https://docs.python.org/3/glossary.html#term-list)\[`_messages.ModelMessage`\] ##### all\_messages\_json ```python def all_messages_json(output_tool_return_content: str | None = None) -> bytes ``` Return all messages from [`all_messages`](/docs/ai/api/pydantic-ai/run/#pydantic_ai.run.AgentRun.all_messages) as JSON bytes. ###### Returns [`bytes`](https://docs.python.org/3/library/stdtypes.html#bytes) -- JSON bytes representing the messages. ##### new\_messages ```python def new_messages() -> list[_messages.ModelMessage] ``` Return the messages produced during this run so far. Messages provided via `message_history` and messages from older runs are excluded. ###### Returns [`list`](https://docs.python.org/3/glossary.html#term-list)\[`_messages.ModelMessage`\] ##### new\_messages\_json ```python def new_messages_json() -> bytes ``` Return new messages from [`new_messages`](/docs/ai/api/pydantic-ai/run/#pydantic_ai.run.AgentRun.new_messages) as JSON bytes. ###### Returns [`bytes`](https://docs.python.org/3/library/stdtypes.html#bytes) -- JSON bytes representing the new messages. ##### \_\_aiter\_\_ ```python def __aiter__( ) -> AsyncIterator[_agent_graph.AgentNode[AgentDepsT, OutputDataT] | End[FinalResult[OutputDataT]]] ``` Provide async-iteration over the nodes in the agent run. ###### Returns [`AsyncIterator`](https://docs.python.org/3/library/typing.html#typing.AsyncIterator)\[`_agent_graph.AgentNode`\[`AgentDepsT`, `OutputDataT`\] | `End`\[`FinalResult`\[`OutputDataT`\]\]\] ##### \_\_anext\_\_ `@async` ```python def __anext__( ) -> _agent_graph.AgentNode[AgentDepsT, OutputDataT] | End[FinalResult[OutputDataT]] ``` Advance to the next node automatically based on the last returned node. Note: this uses the graph run's internal iteration which does NOT call node hooks (`before_node_run`, `wrap_node_run`, `after_node_run`, `on_node_run_error`). Use `next()` for capability-hooked iteration, or use `agent.run()` which drives via `next()` automatically. ###### Returns `_agent_graph.AgentNode`\[`AgentDepsT`, `OutputDataT`\] | `End`\[`FinalResult`\[`OutputDataT`\]\] ##### next `@async` ```python def next( node: _agent_graph.AgentNode[AgentDepsT, OutputDataT], ) -> _agent_graph.AgentNode[AgentDepsT, OutputDataT] | End[FinalResult[OutputDataT]] ``` Manually drive the agent run by passing in the node you want to run next. This lets you inspect or mutate the node before continuing execution, or skip certain nodes under dynamic conditions. The agent run should be stopped when you return an `End` node. Example: ```python from pydantic_ai import Agent from pydantic_graph import End agent = Agent('openai:gpt-5.2') async def main(): async with agent.iter('What is the capital of France?') as agent_run: next_node = agent_run.next_node # start with the first node nodes = [next_node] while not isinstance(next_node, End): next_node = await agent_run.next(next_node) nodes.append(next_node) # Once `next_node` is an End, we've finished: print(nodes) ''' [ UserPromptNode( user_prompt='What is the capital of France?', instructions_functions=[], system_prompts=(), system_prompt_functions=[], system_prompt_dynamic_functions={}, ), ModelRequestNode( request=ModelRequest( parts=[ UserPromptPart( content='What is the capital of France?', timestamp=datetime.datetime(...), ) ], timestamp=datetime.datetime(...), run_id='...', conversation_id='...', ) ), CallToolsNode( model_response=ModelResponse( parts=[TextPart(content='The capital of France is Paris.')], usage=RequestUsage(input_tokens=56, output_tokens=7), model_name='gpt-5.2', timestamp=datetime.datetime(...), run_id='...', conversation_id='...', ) ), End(data=FinalResult(output='The capital of France is Paris.')), ] ''' print('Final result:', agent_run.result.output) #> Final result: The capital of France is Paris. ``` ###### Returns `_agent_graph.AgentNode`\[`AgentDepsT`, `OutputDataT`\] | `End`\[`FinalResult`\[`OutputDataT`\]\] -- The next node returned by the graph logic, or an `End` node if `_agent_graph.AgentNode`\[`AgentDepsT`, `OutputDataT`\] | `End`\[`FinalResult`\[`OutputDataT`\]\] -- the run has completed. ###### Parameters **`node`** : `_agent_graph.AgentNode`\[`AgentDepsT`, `OutputDataT`\] The node to run next in the graph. ##### usage ```python def usage() -> _usage.RunUsage ``` Get usage statistics for the run so far, including token usage, model requests, and so on. ###### Returns `_usage.RunUsage` ### AgentRunResult **Bases:** `Generic[OutputDataT]` The final result of an agent run. #### Attributes ##### output The output data from the agent run. **Type:** `OutputDataT` ##### response Return the last response from the message history. **Type:** `_messages.ModelResponse` ##### metadata Metadata associated with this agent run, if configured. **Type:** [`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] | [`None`](https://docs.python.org/3/library/constants.html#None) ##### run\_id The unique identifier for the agent run. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### conversation\_id The unique identifier for the conversation this run belongs to. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) #### Methods ##### all\_messages ```python def all_messages( output_tool_return_content: str | None = None, ) -> list[_messages.ModelMessage] ``` Return the history of \_messages. ###### Returns [`list`](https://docs.python.org/3/glossary.html#term-list)\[`_messages.ModelMessage`\] -- List of messages. ###### Parameters **`output_tool_return_content`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` The return content of the tool call to set in the last message. This provides a convenient way to modify the content of the output tool call if you want to continue the conversation and want to set the response to the output tool call. If `None`, the last message will not be modified. ##### all\_messages\_json ```python def all_messages_json(output_tool_return_content: str | None = None) -> bytes ``` Return all messages from [`all_messages`](/docs/ai/api/pydantic-ai/run/#pydantic_ai.run.AgentRunResult.all_messages) as JSON bytes. ###### Returns [`bytes`](https://docs.python.org/3/library/stdtypes.html#bytes) -- JSON bytes representing the messages. ###### Parameters **`output_tool_return_content`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` The return content of the tool call to set in the last message. This provides a convenient way to modify the content of the output tool call if you want to continue the conversation and want to set the response to the output tool call. If `None`, the last message will not be modified. ##### new\_messages ```python def new_messages( output_tool_return_content: str | None = None, ) -> list[_messages.ModelMessage] ``` Return the messages produced during this run. Messages provided via `message_history` and messages from older runs are excluded. ###### Returns [`list`](https://docs.python.org/3/glossary.html#term-list)\[`_messages.ModelMessage`\] -- List of new messages. ###### Parameters **`output_tool_return_content`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` The return content of the tool call to set in the last message. This provides a convenient way to modify the content of the output tool call if you want to continue the conversation and want to set the response to the output tool call. If `None`, the last message will not be modified. ##### new\_messages\_json ```python def new_messages_json(output_tool_return_content: str | None = None) -> bytes ``` Return new messages from [`new_messages`](/docs/ai/api/pydantic-ai/run/#pydantic_ai.run.AgentRunResult.new_messages) as JSON bytes. ###### Returns [`bytes`](https://docs.python.org/3/library/stdtypes.html#bytes) -- JSON bytes representing the new messages. ###### Parameters **`output_tool_return_content`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` The return content of the tool call to set in the last message. This provides a convenient way to modify the content of the output tool call if you want to continue the conversation and want to set the response to the output tool call. If `None`, the last message will not be modified. ##### usage ```python def usage() -> _usage.RunUsage ``` Return the usage of the whole run. ###### Returns `_usage.RunUsage` ##### timestamp ```python def timestamp() -> datetime ``` Return the timestamp of last response. ###### Returns [`datetime`](https://docs.python.org/3/library/datetime.html#module-datetime) ### InstrumentationSettings Options for instrumenting models and agents with OpenTelemetry. Used in: - `Agent(instrument=...)` - [`Agent.instrument_all()`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.Agent.instrument_all) - [`InstrumentedModel`](/docs/ai/api/models/instrumented/#pydantic_ai.models.instrumented.InstrumentedModel) See the [Debugging and Monitoring guide](https://ai.pydantic.dev/logfire/) for more info. #### Methods ##### \_\_init\_\_ ```python def __init__( tracer_provider: TracerProvider | None = None, meter_provider: MeterProvider | None = None, include_binary_content: bool = True, include_content: bool = True, version: Literal[1, 2, 3, 4, 5] = DEFAULT_INSTRUMENTATION_VERSION, event_mode: Literal['attributes', 'logs'] = 'attributes', logger_provider: LoggerProvider | None = None, use_aggregated_usage_attribute_names: bool = False, ) ``` Create instrumentation options. ###### Parameters **`tracer_provider`** : `TracerProvider` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` The OpenTelemetry tracer provider to use. If not provided, the global tracer provider is used. Calling `logfire.configure()` sets the global tracer provider, so most users don't need this. **`meter_provider`** : `MeterProvider` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` The OpenTelemetry meter provider to use. If not provided, the global meter provider is used. Calling `logfire.configure()` sets the global meter provider, so most users don't need this. **`include_binary_content`** : [`bool`](https://docs.python.org/3/library/functions.html#bool) _Default:_ `True` Whether to include binary content in the instrumentation events. **`include_content`** : [`bool`](https://docs.python.org/3/library/functions.html#bool) _Default:_ `True` Whether to include prompts, completions, and tool call arguments and responses in the instrumentation events. **`version`** : [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\[1, 2, 3, 4, 5\] _Default:_ `DEFAULT_INSTRUMENTATION_VERSION` Version of the data format. This is unrelated to the Pydantic AI package version. Version 1 is based on the legacy event-based OpenTelemetry GenAI spec and will be removed in a future release. The parameters `event_mode` and `logger_provider` are only relevant for version 1. Version 2 uses the newer OpenTelemetry GenAI spec and stores messages in the following attributes: - `gen_ai.system_instructions` for instructions passed to the agent. - `gen_ai.input.messages` and `gen_ai.output.messages` on model request spans. - `pydantic_ai.all_messages` on agent run spans. Version 3 is the same as version 2, with additional support for thinking tokens. Version 4 is the same as version 3, with GenAI semantic conventions for multimodal content: URL-based media uses type='uri' with uri and mime\_type fields (and modality for image/audio/video). Inline binary content uses type='blob' with mime\_type and content fields (and modality for image/audio/video). [https://opentelemetry.io/docs/specs/semconv/gen-ai/non-normative/examples-llm-calls/#multimodal-inputs-example](https://opentelemetry.io/docs/specs/semconv/gen-ai/non-normative/examples-llm-calls/#multimodal-inputs-example) Version 5 is the same as version 4, but CallDeferred and ApprovalRequired exceptions no longer record an exception event or set the span status to ERROR -- the span is left as UNSET, since deferrals are control flow, not errors. **`event_mode`** : [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['attributes', 'logs'\] _Default:_ `'attributes'` The mode for emitting events in version 1. If `'attributes'`, events are attached to the span as attributes. If `'logs'`, events are emitted as OpenTelemetry log-based events. **`logger_provider`** : `LoggerProvider` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` The OpenTelemetry logger provider to use. If not provided, the global logger provider is used. Calling `logfire.configure()` sets the global logger provider, so most users don't need this. This is only used if `event_mode='logs'` and `version=1`. **`use_aggregated_usage_attribute_names`** : [`bool`](https://docs.python.org/3/library/functions.html#bool) _Default:_ `False` Whether to use `gen_ai.aggregated_usage.*` attribute names for token usage on agent run spans instead of the standard `gen_ai.usage.*` names. Enable this to prevent double-counting in observability backends that aggregate span attributes across parent and child spans. Defaults to False. Note: `gen_ai.aggregated_usage.*` is a custom namespace, not part of the OpenTelemetry Semantic Conventions. It may be updated if OTel introduces an official convention. ##### messages\_to\_otel\_events ```python def messages_to_otel_events( messages: list[ModelMessage], parameters: ModelRequestParameters | None = None, ) -> list[LogRecord] ``` Convert a list of model messages to OpenTelemetry events. ###### Returns [`list`](https://docs.python.org/3/glossary.html#term-list)\[`LogRecord`\] -- A list of OpenTelemetry events. ###### Parameters **`messages`** : [`list`](https://docs.python.org/3/glossary.html#term-list)\[[`ModelMessage`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.ModelMessage)\] The messages to convert. **`parameters`** : `ModelRequestParameters` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` The model request parameters. ### capture\_run\_messages ```python def capture_run_messages() -> Iterator[list[_messages.ModelMessage]] ``` Context manager to access the messages used in a [`run`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.AbstractAgent.run), [`run_sync`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.AbstractAgent.run_sync), or [`run_stream`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.AbstractAgent.run_stream) call. Useful when a run may raise an exception, see [model errors](/docs/ai/core-concepts/agent#model-errors) for more information. Examples: ```python from pydantic_ai import Agent, capture_run_messages agent = Agent('test') with capture_run_messages() as messages: try: result = agent.run_sync('foobar') except Exception: print(messages) raise ``` Note If you call `run`, `run_sync`, or `run_stream` more than once within a single `capture_run_messages` context, `messages` will represent the messages exchanged during the first call only. #### Returns [`Iterator`](https://docs.python.org/3/library/typing.html#typing.Iterator)\[[`list`](https://docs.python.org/3/glossary.html#term-list)\[`_messages.ModelMessage`\]\] ### EndStrategy **Default:** `Literal['early', 'graceful', 'exhaustive']` ### RunOutputDataT Type variable for the result data of a run where `output_type` was customized on the run call. **Default:** `TypeVar('RunOutputDataT')` ### EventStreamHandler A function that receives agent [`RunContext`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.RunContext) and an async iterable of events from the model's streaming response and the agent's execution of tools. **Type:** [`TypeAlias`](https://docs.python.org/3/library/typing.html#typing.TypeAlias) **Default:** `Callable[[RunContext[AgentDepsT], AsyncIterable[_messages.AgentStreamEvent]], Awaitable[None]]` --- # [pydantic_ai.capabilities](https://pydantic.dev/docs/ai/api/pydantic-ai/capabilities/) # pydantic\_ai.capabilities ### Toolset **Bases:** `AbstractCapability[AgentDepsT]` A capability that provides a toolset. ### Thinking **Bases:** `AbstractCapability[Any]` Enables and configures model thinking/reasoning. Uses the unified `thinking` setting in [`ModelSettings`](/docs/ai/api/pydantic-ai/settings/#pydantic_ai.settings.ModelSettings) to work portably across providers. Provider-specific thinking settings (e.g., `anthropic_thinking`, `openai_reasoning_effort`) take precedence when both are set. #### Attributes ##### effort The thinking effort level. - `True`: Enable thinking with the provider's default effort. - `False`: Disable thinking (silently ignored on always-on models). - `'minimal'`/`'low'`/`'medium'`/`'high'`/`'xhigh'`: Enable thinking at a specific effort level. **Type:** `ThinkingLevel` **Default:** `True` ### PrepareTools **Bases:** `AbstractCapability[AgentDepsT]` Capability that filters or modifies function tool definitions using a callable. Wraps a [`ToolsPrepareFunc`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.ToolsPrepareFunc) as a capability. Filters/modifies **function** tools only; for output tools use [`PrepareOutputTools`](/docs/ai/api/pydantic-ai/capabilities/#pydantic_ai.capabilities.PrepareOutputTools). ```python from pydantic_ai import Agent, RunContext from pydantic_ai.capabilities import PrepareTools from pydantic_ai.tools import ToolDefinition async def hide_admin_tools( ctx: RunContext[None], tool_defs: list[ToolDefinition] ) -> list[ToolDefinition] | None: return [td for td in tool_defs if not td.name.startswith('admin_')] agent = Agent('openai:gpt-5', capabilities=[PrepareTools(hide_admin_tools)]) ``` ### HandleDeferredToolCalls **Bases:** `AbstractCapability[AgentDepsT]` Resolves deferred tool calls inline during an agent run using a handler function. When tools require approval or external execution, the agent normally pauses the run and returns [`DeferredToolRequests`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.DeferredToolRequests) as output. This capability intercepts deferred tool calls, calls the provided handler to resolve them, and continues the agent run automatically. The handler receives the [`RunContext`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.RunContext) and the [`DeferredToolRequests`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.DeferredToolRequests). It may return [`DeferredToolResults`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.DeferredToolResults) with results for some or all pending calls, or return `None` to decline handling (the next capability in the chain gets a chance, otherwise the calls bubble up as `DeferredToolRequests` output). #### Attributes ##### handler The handler function that resolves deferred tool requests. Receives the run context and the deferred tool requests, and returns [`DeferredToolResults`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.DeferredToolResults) with results for some or all pending calls, or `None` to decline handling. Can be sync or async. **Type:** [`Callable`](https://docs.python.org/3/library/typing.html#typing.Callable)\[\[[`RunContext`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.RunContext)\[`AgentDepsT`\], [`DeferredToolRequests`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.DeferredToolRequests)\], [`DeferredToolResults`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.DeferredToolResults) | [`None`](https://docs.python.org/3/library/constants.html#None) | [`Awaitable`](https://docs.python.org/3/library/typing.html#typing.Awaitable)\[[`DeferredToolResults`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.DeferredToolResults) | [`None`](https://docs.python.org/3/library/constants.html#None)\]\] ### IncludeToolReturnSchemas **Bases:** `AbstractCapability[AgentDepsT]` Capability that includes return schemas for selected tools. When added to an agent's capabilities, this sets [`include_return_schema`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.ToolDefinition.include_return_schema) to `True` on matching tool definitions, causing the model to receive return type information for those tools. For models that natively support return schemas (e.g. Google Gemini), the schema is passed as a structured field. For other models, it is injected into the tool description as JSON text. Per-tool overrides (`Tool(..., include_return_schema=False)`) take precedence -- this capability only sets the flag on tools that haven't explicitly opted out. ```python from pydantic_ai import Agent from pydantic_ai.capabilities import IncludeToolReturnSchemas agent = Agent('openai:gpt-5', capabilities=[IncludeToolReturnSchemas()]) ``` #### Attributes ##### tools Which tools should have their return schemas included. - `'all'` (default): every tool gets its return schema included. - `Sequence[str]`: only tools whose names are listed. - `dict[str, Any]`: matches tools whose metadata deeply includes the specified key-value pairs. - Callable `(ctx, tool_def) -> bool`: custom sync or async predicate. **Type:** `ToolSelector`\[`AgentDepsT`\] **Default:** `'all'` ### PrefixTools **Bases:** `WrapperCapability[AgentDepsT]` A capability that wraps another capability and prefixes its tool names. Only the wrapped capability's tools are prefixed; other agent tools are unaffected. ```python from pydantic_ai import Agent from pydantic_ai.capabilities import PrefixTools, Toolset from pydantic_ai.toolsets import FunctionToolset toolset = FunctionToolset() agent = Agent( 'openai:gpt-5', capabilities=[ PrefixTools( wrapped=Toolset(toolset), prefix='ns', ), ], ) ``` #### Methods ##### from\_spec `@classmethod` ```python def from_spec(cls, prefix: str, capability: CapabilitySpec) -> PrefixTools[Any] ``` Create from spec with a nested capability specification. ###### Returns `PrefixTools`\[[`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] ###### Parameters **`prefix`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) The prefix to add to tool names (e.g. `'mcp'` turns `'search'` into `'mcp_search'`). **`capability`** : `CapabilitySpec` A capability spec (same format as entries in the `capabilities` list). ### SetToolMetadata **Bases:** `AbstractCapability[AgentDepsT]` Capability that merges metadata key-value pairs onto selected tools. ```python from pydantic_ai import Agent from pydantic_ai.capabilities import SetToolMetadata agent = Agent('openai:gpt-5', capabilities=[SetToolMetadata(code_mode=True)]) ``` ### ThreadExecutor **Bases:** `AbstractCapability[Any]` Use a custom executor for running sync functions in threads. By default, sync tool functions and other sync callbacks are run in threads using `anyio.to_thread.run_sync`, which creates ephemeral threads. In long-running servers (e.g. FastAPI), this can lead to thread accumulation under sustained load. This capability provides a bounded [`ThreadPoolExecutor`](https://docs.python.org/3/library/concurrent.futures.html#concurrent.futures.ThreadPoolExecutor) (or any [`Executor`](https://docs.python.org/3/library/concurrent.futures.html#concurrent.futures.Executor)) to use instead, scoped to agent runs: ```python from concurrent.futures import ThreadPoolExecutor from pydantic_ai import Agent from pydantic_ai.capabilities import ThreadExecutor executor = ThreadPoolExecutor(max_workers=16, thread_name_prefix='agent-worker') agent = Agent('openai:gpt-5.2', capabilities=[ThreadExecutor(executor)]) ``` To set an executor for all agents globally, use [`Agent.using_thread_executor()`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.AbstractAgent.using_thread_executor). #### Attributes ##### executor The executor to use for running sync functions. **Type:** `Executor` ### NativeTool **Bases:** `AbstractCapability[AgentDepsT]` A capability that registers a native tool with the agent. Wraps a single [`AgentNativeTool`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.AgentNativeTool) -- either a static [`AbstractNativeTool`](/docs/ai/api/pydantic-ai/native_tools/#pydantic_ai.native_tools.AbstractNativeTool) instance or a callable that dynamically produces one. Equivalent to passing the tool through `Agent(capabilities=[NativeTool(my_tool)])`. For provider-adaptive use (with a local fallback), see [`NativeOrLocalTool`](/docs/ai/api/pydantic-ai/capabilities/#pydantic_ai.capabilities.NativeOrLocalTool) or its subclasses like [`WebSearch`](/docs/ai/api/pydantic-ai/capabilities/#pydantic_ai.capabilities.WebSearch). #### Methods ##### from\_spec `@classmethod` ```python def from_spec( cls, tool: AbstractNativeTool | None = None, kwargs: Any = {}, ) -> NativeTool[Any] ``` Create from spec. Supports two YAML forms: - Flat: `{NativeTool: {kind: web_search, search_context_size: high}}` - Explicit: `{NativeTool: {tool: {kind: web_search}}}` ###### Returns `NativeTool`\[[`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] ### WebFetch **Bases:** `NativeOrLocalTool[AgentDepsT]` URL fetching capability. Uses the model's native URL fetching when available, falling back to a local function tool (markdownify-based fetch by default) when it isn't. The local fallback requires the `web-fetch` optional group: Terminal ```bash pip install "pydantic-ai-slim[web-fetch]" ``` #### Attributes ##### allowed\_domains Only fetch from these domains. Enforced locally when native is unavailable. **Type:** [`list`](https://docs.python.org/3/glossary.html#term-list)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str)\] | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `allowed_domains` ##### blocked\_domains Never fetch from these domains. Enforced locally when native is unavailable. **Type:** [`list`](https://docs.python.org/3/glossary.html#term-list)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str)\] | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `blocked_domains` ##### max\_uses Maximum number of fetches per run. Requires native support. **Type:** [`int`](https://docs.python.org/3/library/functions.html#int) | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `max_uses` ##### enable\_citations Enable citations for fetched content. Native-only; ignored by local tools. **Type:** [`bool`](https://docs.python.org/3/library/functions.html#bool) | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `enable_citations` ##### max\_content\_tokens Maximum content length in tokens. Native-only; ignored by local tools. **Type:** [`int`](https://docs.python.org/3/library/functions.html#int) | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `max_content_tokens` ### DynamicCapability **Bases:** `AbstractCapability[AgentDepsT]` A capability that builds another capability dynamically using a function that takes the run context. The factory is called once per agent run from [`for_run`](/docs/ai/api/pydantic-ai/capabilities/#pydantic_ai.capabilities.AbstractCapability.for_run). The returned capability replaces this wrapper for the rest of the run, so its instructions, model settings, toolset, native tools, and hooks all flow through normally. Pass a [`CapabilityFunc`](/docs/ai/api/pydantic-ai/capabilities/#pydantic_ai.capabilities.CapabilityFunc) directly to `Agent(capabilities=[...])` or `agent.run(capabilities=[...])` and it will be wrapped in a `DynamicCapability` automatically. #### Attributes ##### capability\_func The function that takes the run context and returns a capability or `None`. **Type:** [`CapabilityFunc`](/docs/ai/api/pydantic-ai/capabilities/#pydantic_ai.capabilities.CapabilityFunc)\[`AgentDepsT`\] ### ImageGeneration **Bases:** `NativeOrLocalTool[AgentDepsT]` Image generation capability. Uses the model's native image generation when available. When the model doesn't support it and `fallback_model` is provided, falls back to a local tool that delegates to a subagent running the specified image-capable model. Image generation settings (`quality`, `size`, etc.) are forwarded to the [`ImageGenerationTool`](/docs/ai/api/pydantic-ai/native_tools/#pydantic_ai.native_tools.ImageGenerationTool) used by both the native and the local fallback subagent. When passing a custom `native` instance, its settings are also used for the fallback subagent; capability-level fields override any `native` instance settings. #### Attributes ##### fallback\_model Model to use for image generation when the agent's model doesn't support it natively. Must be a model that supports image generation via the [`ImageGenerationTool`](/docs/ai/api/pydantic-ai/native_tools/#pydantic_ai.native_tools.ImageGenerationTool) native tool. This requires a conversational model with image generation support, not a dedicated image-only API. Examples: - `'openai-responses:gpt-5.4'` -- OpenAI model with image generation support - `'google:gemini-3-pro-image-preview'` -- Google image generation model Can be a model name string, `Model` instance, or a callable taking `RunContext` that returns a `Model` instance. **Type:** `ImageGenerationFallbackModel` **Default:** `fallback_model` ##### action Whether to generate a new image or edit an existing image. Supported by: OpenAI Responses. Default: `'auto'`. **Type:** [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['generate', 'edit', 'auto'\] | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `action` ##### background Background type for the generated image. Supported by: OpenAI Responses. `'transparent'` only supported for `'png'` and `'webp'`. **Type:** [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['transparent', 'opaque', 'auto'\] | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `background` ##### input\_fidelity Input fidelity for matching style/features of input images. Supported by: OpenAI Responses. Default: `'low'`. **Type:** [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['high', 'low'\] | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `input_fidelity` ##### moderation Moderation level for the generated image. Supported by: OpenAI Responses. **Type:** [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['auto', 'low'\] | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `moderation` ##### image\_model The image generation model to use. Supported by: OpenAI Responses. **Type:** `ImageGenerationModelName` | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `image_model` ##### output\_compression Compression level for the output image. Supported by: OpenAI Responses (jpeg/webp, default: 100), Google Cloud (jpeg, default: 75). **Type:** [`int`](https://docs.python.org/3/library/functions.html#int) | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `output_compression` ##### output\_format Output format of the generated image. Supported by: OpenAI Responses (default: `'png'`), Google Cloud. **Type:** [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['png', 'webp', 'jpeg'\] | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `output_format` ##### quality Quality of the generated image. Supported by: OpenAI Responses. **Type:** [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['low', 'medium', 'high', 'auto'\] | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `quality` ##### size Size of the generated image. Supported by: OpenAI Responses (`'auto'`, `'1024x1024'`, `'1024x1536'`, `'1536x1024'`), Google (`'512'`, `'1K'`, `'2K'`, `'4K'`). **Type:** [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['auto', '1024x1024', '1024x1536', '1536x1024', '512', '1K', '2K', '4K'\] | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `size` ##### aspect\_ratio Aspect ratio for generated images. Supported by: Google (Gemini), OpenAI Responses (maps `'1:1'`, `'2:3'`, `'3:2'` to sizes). **Type:** `ImageAspectRatio` | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `aspect_ratio` ### NativeOrLocalTool **Bases:** `AbstractCapability[AgentDepsT]` Capability that pairs a provider-native tool with a local fallback. When the model supports the native tool, the local fallback is removed. When the model doesn't support the native tool, it is removed and the local tool stays. Can be used directly: ```python from pydantic_ai.capabilities import NativeOrLocalTool cap = NativeOrLocalTool(native=WebSearchTool(), local=my_search_func) ``` Or subclassed to set defaults by overriding `_default_native`, `_default_local`, and `_requires_native`. The built-in [`WebSearch`](/docs/ai/api/pydantic-ai/capabilities/#pydantic_ai.capabilities.WebSearch), [`WebFetch`](/docs/ai/api/pydantic-ai/capabilities/#pydantic_ai.capabilities.WebFetch), and [`ImageGeneration`](/docs/ai/api/pydantic-ai/capabilities/#pydantic_ai.capabilities.ImageGeneration) capabilities are all subclasses. #### Attributes ##### native Configure the provider-native tool. - `True` (default): use the default native tool configuration (subclasses only). - `False`: disable the native tool; always use the local tool. - An `AbstractNativeTool` instance: use this specific configuration. - A callable (`NativeToolFunc`): dynamically create the native tool per-run via `RunContext`. **Type:** [`AgentNativeTool`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.AgentNativeTool)\[`AgentDepsT`\] | [`bool`](https://docs.python.org/3/library/functions.html#bool) **Default:** `native` ##### local Configure the local fallback tool. - `None` (default): auto-detect a local fallback via `_default_local`. - `True`: opt in to the default local fallback (resolved via `_resolve_local_strategy`). - `False`: disable the local fallback; only use the native tool. - A named strategy (e.g. `'duckduckgo'`): resolved via `_resolve_local_strategy` in subclasses. - A `Tool` or `AbstractToolset` instance: use this specific local tool. - A bare callable: automatically wrapped in a `Tool`. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`Tool`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.Tool)\[`AgentDepsT`\] | [`Callable`](https://docs.python.org/3/library/typing.html#typing.Callable)\[..., [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] | [`AbstractToolset`](/docs/ai/api/pydantic-ai/toolsets/#pydantic_ai.toolsets.AbstractToolset)\[`AgentDepsT`\] | [`bool`](https://docs.python.org/3/library/functions.html#bool) | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `local` ### ProcessEventStream **Bases:** `AbstractCapability[AgentDepsT]` A capability that forwards the agent's event stream to a user-provided async handler. The handler receives the stream of [`AgentStreamEvent`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.AgentStreamEvent)s emitted during model streaming and tool execution for each `ModelRequestNode` and `CallToolsNode`. Two forms are supported: - An [`EventStreamHandler`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.EventStreamHandler) -- an `async def` returning `None`. Events are forwarded to the handler while also being passed through unchanged to the rest of the capability chain, so multiple handlers (and the top-level `event_stream_handler` argument) can all see the same stream without changing each other's view. A handler that returns early stops receiving events but does not affect downstream consumers; a handler that raises propagates the exception to the rest of the run. Events are delivered synchronously, so a slow handler back-pressures the rest of the stream. - An `EventStreamProcessor` -- an async generator yielding [`AgentStreamEvent`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.AgentStreamEvent)s. The events it yields replace the inner stream for downstream wrappers and consumers, so it can modify, drop, or add events. When this capability is registered, `agent.run()` automatically enables streaming so the handler fires without requiring an explicit `event_stream_handler` argument. Durable execution Under the current durable-execution integrations ([Temporal](/docs/ai/api/pydantic-ai/durable_exec/#pydantic_ai.durable_exec.temporal.TemporalAgent), [DBOS](/docs/ai/api/pydantic-ai/durable_exec/#pydantic_ai.durable_exec.dbos.DBOSAgent), [Prefect](/docs/ai/api/pydantic-ai/durable_exec/#pydantic_ai.durable_exec.prefect.PrefectAgent)), model streaming happens inside an activity/step rather than in the outer agent loop. This capability's `wrap_run_event_stream` hook fires for tool-call events and the final post-streaming batch, but it does **not** see individual model-response events live -- the underlying durable model consumes those inside the activity before returning. The in-flight `event_stream_handler` parameter does still observe the live events; a future refactor threading the capability chain through the activity boundary is being explored in [#4977](https://github.com/pydantic/pydantic-ai/pull/4977). ### WebSearch **Bases:** `NativeOrLocalTool[AgentDepsT]` Web search capability. Uses the model's native web search when available, falling back to a local function tool (DuckDuckGo by default) when it isn't. #### Attributes ##### search\_context\_size Controls how much context is retrieved from the web. Native-only; ignored by local tools. **Type:** [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['low', 'medium', 'high'\] | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `search_context_size` ##### user\_location Localize search results based on user location. Native-only; ignored by local tools. **Type:** [`WebSearchUserLocation`](/docs/ai/api/pydantic-ai/native_tools/#pydantic_ai.native_tools.WebSearchUserLocation) | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `user_location` ##### blocked\_domains Domains to exclude from results. Requires native support. **Type:** [`list`](https://docs.python.org/3/glossary.html#term-list)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str)\] | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `blocked_domains` ##### allowed\_domains Only include results from these domains. Requires native support. **Type:** [`list`](https://docs.python.org/3/glossary.html#term-list)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str)\] | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `allowed_domains` ##### max\_uses Maximum number of web searches per run. Requires native support. **Type:** [`int`](https://docs.python.org/3/library/functions.html#int) | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `max_uses` ### ReinjectSystemPrompt **Bases:** `AbstractCapability[AgentDepsT]` Capability that reinjects the agent's configured `system_prompt` when missing from history. Ensures the agent's configured `system_prompt` is present at the head of the first `ModelRequest` on every model request. Intended for callers that reconstruct a `message_history` from a source that doesn't round-trip system prompts -- UI frontends, database persistence layers, conversation compaction pipelines. By default, if any `SystemPromptPart` is already present anywhere in the history (for example, preserved from a prior run or handed off from another agent), this capability leaves the messages untouched so that existing system prompts remain authoritative. Set `replace_existing=True` to instead strip any existing `SystemPromptPart`s before prepending the agent's configured prompt -- useful when the history comes from an untrusted source (such as a UI frontend) and the server's prompt must win. The UI adapters automatically add this capability in `manage_system_prompt='server'` mode with `replace_existing=True`. Add it explicitly with `Agent(..., capabilities=[ReinjectSystemPrompt()])` or per-run via the `capabilities=` argument on [`Agent.run`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.AbstractAgent.run) to get the same behavior anywhere. #### Attributes ##### replace\_existing If `True`, strip any existing `SystemPromptPart`s from the history before prepending the agent's configured prompt. If `False` (the default), the capability is a no-op when any `SystemPromptPart` is already present. **Type:** [`bool`](https://docs.python.org/3/library/functions.html#bool) **Default:** `False` ### ProcessHistory **Bases:** `AbstractCapability[AgentDepsT]` A capability that processes message history before model requests. ### ToolSearch **Bases:** `AbstractCapability[AgentDepsT]` Capability that provides tool discovery for large toolsets. Tools marked with `defer_loading=True` are hidden from the model until discovered. Auto-injected into every agent -- zero overhead when no deferred tools exist. When the model supports native tool search (Anthropic BM25/regex, OpenAI Responses), discovery is handled by the provider: the deferred tools are sent with `defer_loading` on the wire and the provider exposes them once they've been discovered. Otherwise, discovery happens locally via a `search_tools` function that the model can call. On providers that support a native "client-executed" surface (Anthropic, OpenAI), the discovery message is delivered append-only -- prompt cache is preserved across discovery turns, so growing the message history with discovered-tool results does not invalidate the cached prefix. ```python from collections.abc import Sequence from pydantic_ai import Agent, RunContext, Tool from pydantic_ai.capabilities import ToolSearch from pydantic_ai.tools import ToolDefinition # Tools become deferred via `defer_loading=True`. They stay hidden from the model # until tool search discovers them. def get_weather(city: str) -> str: ... weather_tool = Tool(get_weather, defer_loading=True) # Default: native search on supporting providers, local keyword matching elsewhere. agent = Agent('anthropic:claude-sonnet-4-6', tools=[weather_tool], capabilities=[ToolSearch()]) # Force a specific Anthropic native strategy; errors on providers that can't honor it. agent = Agent( 'anthropic:claude-sonnet-4-6', tools=[weather_tool], capabilities=[ToolSearch(strategy='regex')], ) # Always run the local keyword-overlap algorithm, regardless of provider. agent = Agent( 'anthropic:claude-sonnet-4-6', tools=[weather_tool], capabilities=[ToolSearch(strategy='keywords')], ) # Custom search function -- used locally, and by provider-native "client-executed" # modes when supported. def my_search( ctx: RunContext[None], queries: Sequence[str], tools: Sequence[ToolDefinition] ) -> list[str]: return [ t.name for t in tools if any(q.lower() in (t.description or '').lower() for q in queries) ] agent = Agent( 'anthropic:claude-sonnet-4-6', tools=[weather_tool], capabilities=[ToolSearch(strategy=my_search)], ) ``` #### Attributes ##### strategy The search strategy to use. - `None` (default): let Pydantic AI pick the best strategy for the current provider -- native on supporting models (Anthropic BM25, OpenAI server-executed tool search), local keyword matching elsewhere. The choice may change in future versions. - `'keywords'`: always use the local keyword-overlap algorithm. Still prompt-cache compatible on providers that expose a "client-executed" native surface (Anthropic, OpenAI): the algorithm rides the same `defer_loading` wire as a custom callable, so the tool list stays stable across discovery rounds and the cached prefix is preserved. - `'bm25'` / `'regex'`: force a specific Anthropic native strategy. Raises on providers that can't honor the choice (including OpenAI, which has no named native strategies). - Callable `(ctx, queries, tools) -> names`: custom search function (sync or async). Used locally, and by the native "client-executed" surface on providers that support it (Anthropic custom tool-reference blocks, OpenAI `execution='client'`). **Type:** `ToolSearchStrategy`\[`AgentDepsT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `None` ##### max\_results Maximum number of matches returned by the local search algorithm. **Type:** [`int`](https://docs.python.org/3/library/functions.html#int) **Default:** `10` ##### tool\_description Custom description for the local `search_tools` function shown to the model. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `None` ##### parameter\_description Custom description for the `queries` parameter on the local `search_tools` function. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `None` ### MCP **Bases:** `NativeOrLocalTool[AgentDepsT]` MCP server capability. Uses the model's native MCP server support when available, connecting directly via HTTP when it isn't. #### Attributes ##### url The URL of the MCP server. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) **Default:** `url` ##### id Unique identifier for the MCP server. Defaults to a slug derived from the URL. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `id` ##### authorization\_token Authorization header value for MCP server requests. Passed to both native and local. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `authorization_token` ##### headers HTTP headers for MCP server requests. Passed to both native and local. **Type:** [`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), [`str`](https://docs.python.org/3/library/stdtypes.html#str)\] | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `headers` ##### allowed\_tools Filter to only these tools. Applied to both native and local. **Type:** [`list`](https://docs.python.org/3/glossary.html#term-list)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str)\] | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `allowed_tools` ##### description Description of the MCP server. Native-only; ignored by local tools. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `description` #### Methods ##### from\_spec `@classmethod` ```python def from_spec( cls, url: str, native: MCPServerTool | bool = True, local: str | bool | None = None, id: str | None = None, authorization_token: str | None = None, headers: dict[str, str] | None = None, allowed_tools: list[str] | None = None, description: str | None = None, ) -> MCP[AgentDepsT] ``` Construct an `MCP` capability from spec-serializable args. Restricts the runtime-wide `local=` union to the JSON/YAML-serializable subset (`str | bool | None`) so `AgentSpec` schema generation works. Non-serializable runtime values like `fastmcp.Client`, `ClientTransport`, or pre-built `MCPToolset` instances can still be passed to `MCP(...)` directly -- they just can't roundtrip through a spec file. ###### Returns `MCP`\[`AgentDepsT`\] ### CombinedCapability **Bases:** `AbstractCapability[AgentDepsT]` A capability that combines multiple capabilities. ### WrapperCapability **Bases:** `AbstractCapability[AgentDepsT]` A capability that wraps another capability and delegates all methods. Analogous to [`WrapperToolset`](/docs/ai/api/pydantic-ai/toolsets/#pydantic_ai.toolsets.WrapperToolset) for toolsets. Subclass and override specific methods to modify behavior while delegating the rest. ### PrepareOutputTools **Bases:** `AbstractCapability[AgentDepsT]` Capability that filters or modifies output tool definitions using a callable. Mirrors [`PrepareTools`](/docs/ai/api/pydantic-ai/capabilities/#pydantic_ai.capabilities.PrepareTools) for [output tools](/docs/ai/api/pydantic-ai/output/#pydantic_ai.output.ToolOutput). `ctx.retry`/`ctx.max_retries` reflect the **output** retry budget (`max_output_retries`), matching the output hook lifecycle. ```python from pydantic_ai import Agent, RunContext from pydantic_ai.capabilities import PrepareOutputTools from pydantic_ai.output import ToolOutput from pydantic_ai.tools import ToolDefinition async def only_after_first_step( ctx: RunContext[None], tool_defs: list[ToolDefinition] ) -> list[ToolDefinition] | None: return tool_defs if ctx.run_step > 0 else [] agent = Agent( 'openai:gpt-5', output_type=ToolOutput(str), capabilities=[PrepareOutputTools(only_after_first_step)], ) ``` ### HistoryProcessor **Bases:** `ProcessHistory[AgentDepsT]` Deprecated alias for [`ProcessHistory`](/docs/ai/api/pydantic-ai/capabilities/#pydantic_ai.capabilities.ProcessHistory). ### Instrumentation **Bases:** `AbstractCapability[Any]` Capability that instruments agent runs with OpenTelemetry/Logfire tracing. When added to an agent via `capabilities=[Instrumentation(...)]`, this capability creates OpenTelemetry spans for the agent run, model requests, and tool executions. Other capabilities can add attributes to these spans using the OpenTelemetry API (`opentelemetry.trace.get_current_span().set_attribute(key, value)`). #### Attributes ##### settings OTel/Logfire instrumentation settings. Defaults to `InstrumentationSettings()`, which uses the global `TracerProvider`/`LoggerProvider` (typically configured by `logfire.configure()`). **Type:** [`InstrumentationSettings`](/docs/ai/api/models/instrumented/#pydantic_ai.models.instrumented.InstrumentationSettings) **Default:** `field(default_factory=(lambda: _default_settings()))` #### Methods ##### from\_spec `@classmethod` ```python def from_spec(cls, kwargs: Any = {}) -> Instrumentation ``` Build an `Instrumentation` capability from a YAML/JSON spec. Accepts the serializable subset of [`InstrumentationSettings`](/docs/ai/api/models/instrumented/#pydantic_ai.models.instrumented.InstrumentationSettings) kwargs (`include_binary_content`, `include_content`, `version`, `event_mode`, `use_aggregated_usage_attribute_names`). The OTel `tracer_provider`, `meter_provider`, and `logger_provider` fields can't be expressed in YAML and default to the global providers (typically configured via `logfire.configure()`). YAML form: capabilities: - Instrumentation: {} # default settings - Instrumentation: version: 2 include\_content: false ###### Returns `Instrumentation` ##### for\_run `@async` ```python def for_run(ctx: RunContext[Any]) -> Instrumentation ``` Return a fresh copy for per-run state isolation. ###### Returns `Instrumentation` ##### wrap\_output\_process `@async` ```python def wrap_output_process( ctx: RunContext[AgentDepsT], output_context: OutputContext, output: Any, handler: WrapOutputProcessHandler, ) -> Any ``` Emit a span for output-function execution. Output processing for plain validation (no function) is not span-worthy -- the validated value is the model's response itself, no user code ran. We open a span only when an output function will execute, regardless of whether the output arrived via a tool call. The span name reflects the function (or tool name when the function name is unavailable, e.g. union processors). ###### Returns [`Any`](https://docs.python.org/3/library/typing.html#typing.Any) ### HookTimeoutError **Bases:** [`TimeoutError`](https://docs.python.org/3/library/exceptions.html#TimeoutError) Raised when a hook function exceeds its configured timeout. ### CapabilityOrdering Ordering constraints for a capability within a combined capability chain. Capabilities follow middleware semantics: the first capability in the list is the **outermost** layer, wrapping all others. Declare ordering constraints via [`get_ordering`](/docs/ai/api/pydantic-ai/capabilities/#pydantic_ai.capabilities.AbstractCapability.get_ordering) to control a capability's position in the chain regardless of how the user lists them. When a [`CombinedCapability`](/docs/ai/api/pydantic-ai/capabilities/#pydantic_ai.capabilities.CombinedCapability) is constructed, it topologically sorts its children to satisfy these constraints, preserving user-provided order as a tiebreaker. #### Attributes ##### position Fixed position in the chain, or `None` for user-provided order. **Type:** `CapabilityPosition` | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `None` ##### wraps This capability wraps around (is outside of) these capabilities in the middleware chain. Each entry can be a capability **type** (matches all instances of that type via `issubclass`) or a specific capability **instance** (matches by identity via `is`). Note: instance refs use identity (`is`) matching, so if a capability's [`for_run`](/docs/ai/api/pydantic-ai/capabilities/#pydantic_ai.capabilities.AbstractCapability.for_run) returns a new instance, refs to the original will no longer match. Use type refs when the target capability uses per-run state isolation. **Type:** [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[`CapabilityRef`\] **Default:** `()` ##### wrapped\_by This capability is wrapped by (is inside of) these capabilities in the middleware chain. Each entry can be a capability **type** (matches all instances of that type via `issubclass`) or a specific capability **instance** (matches by identity via `is`). Note: instance refs use identity (`is`) matching, so if a capability's [`for_run`](/docs/ai/api/pydantic-ai/capabilities/#pydantic_ai.capabilities.AbstractCapability.for_run) returns a new instance, refs to the original will no longer match. Use type refs when the target capability uses per-run state isolation. **Type:** [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[`CapabilityRef`\] **Default:** `()` ##### requires These types must be present in the chain (no ordering implied). **Type:** [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`type`](https://docs.python.org/3/glossary.html#term-type)\[`AbstractCapability`\[[`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\]\]\] **Default:** `()` ### AbstractCapability **Bases:** `ABC`, `Generic[AgentDepsT]` Abstract base class for agent capabilities. A capability is a reusable, composable unit of agent behavior that can provide instructions, model settings, tools, and request/response hooks. Lifecycle: capabilities are passed to an [`Agent`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.Agent) at construction time, where most `get_*` methods are called to collect static configuration (instructions, model settings, toolsets, native tools). The exception is [`get_wrapper_toolset`](/docs/ai/api/pydantic-ai/capabilities/#pydantic_ai.capabilities.AbstractCapability.get_wrapper_toolset), which is called per-run during toolset assembly. Then, on each model request during a run, the [`before_model_request`](/docs/ai/api/pydantic-ai/capabilities/#pydantic_ai.capabilities.AbstractCapability.before_model_request) and [`after_model_request`](/docs/ai/api/pydantic-ai/capabilities/#pydantic_ai.capabilities.AbstractCapability.after_model_request) hooks are called to allow dynamic adjustments. See the [capabilities documentation](/docs/ai/api/pydantic-ai/capabilities) for built-in capabilities. [`get_serialization_name`](/docs/ai/api/pydantic-ai/capabilities/#pydantic_ai.capabilities.AbstractCapability.get_serialization_name) and [`from_spec`](/docs/ai/api/pydantic-ai/capabilities/#pydantic_ai.capabilities.AbstractCapability.from_spec) support YAML/JSON specs (via [`Agent.from_spec`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.Agent.from_spec)); they have sensible defaults and typically don't need to be overridden. #### Attributes ##### has\_wrap\_node\_run Whether this capability (or any sub-capability) overrides wrap\_node\_run. **Type:** [`bool`](https://docs.python.org/3/library/functions.html#bool) ##### has\_wrap\_run\_event\_stream Whether this capability (or any sub-capability) overrides wrap\_run\_event\_stream. **Type:** [`bool`](https://docs.python.org/3/library/functions.html#bool) #### Methods ##### apply ```python def apply(visitor: Callable[[AbstractCapability[AgentDepsT]], None]) -> None ``` Run a visitor function on all leaf capabilities in this tree. For a single capability, calls the visitor on itself. Overridden by [`CombinedCapability`](/docs/ai/api/pydantic-ai/capabilities/#pydantic_ai.capabilities.CombinedCapability) to recursively visit all child capabilities, and by [`WrapperCapability`](/docs/ai/api/pydantic-ai/capabilities/#pydantic_ai.capabilities.WrapperCapability) to delegate to the wrapped capability. ###### Returns [`None`](https://docs.python.org/3/library/constants.html#None) ##### get\_serialization\_name `@classmethod` ```python def get_serialization_name(cls) -> str | None ``` Return the name used for spec serialization (CamelCase class name by default). Return None to opt out of spec-based construction. ###### Returns [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) ##### from\_spec `@classmethod` ```python def from_spec(cls, args: Any = (), kwargs: Any = {}) -> AbstractCapability[Any] ``` Create from spec arguments. Default: `cls(*args, **kwargs)`. Override when `__init__` takes non-serializable types. ###### Returns `AbstractCapability`\[[`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] ##### get\_ordering ```python def get_ordering() -> CapabilityOrdering | None ``` Return ordering constraints for this capability, or `None` for default behavior. Override to declare a fixed position (`'outermost'` / `'innermost'`), relative ordering (`wraps` / `wrapped_by` other capability types or instances), or dependency requirements (`requires`). [`CombinedCapability`](/docs/ai/api/pydantic-ai/capabilities/#pydantic_ai.capabilities.CombinedCapability) uses these to topologically sort its children at construction time. ###### Returns `CapabilityOrdering` | [`None`](https://docs.python.org/3/library/constants.html#None) ##### for\_run `@async` ```python def for_run(ctx: RunContext[AgentDepsT]) -> AbstractCapability[AgentDepsT] ``` Return the capability instance to use for this agent run. Called once per run, before `get_*()` re-extraction and before any hooks fire. Override to return a fresh instance for per-run state isolation. Default: return `self` (shared across runs). ###### Returns `AbstractCapability`\[`AgentDepsT`\] ##### get\_instructions ```python def get_instructions() -> AgentInstructions[AgentDepsT] | None ``` Return instructions to include in the system prompt, or None. This method is called once at agent construction time. To get dynamic per-request behavior, return a callable that receives [`RunContext`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.RunContext) or a `TemplateStr` -- not a dynamic string. ###### Returns `AgentInstructions`\[`AgentDepsT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) ##### get\_model\_settings ```python def get_model_settings() -> AgentModelSettings[AgentDepsT] | None ``` Return model settings to merge into the agent's defaults, or None. This method is called once at agent construction time. Return a static `ModelSettings` dict when the settings don't change between requests. Return a callable that receives [`RunContext`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.RunContext) when settings need to vary per step (e.g. based on `ctx.run_step` or `ctx.deps`). When the callable is invoked, `ctx.model_settings` contains the merged result of all layers resolved before this capability (model defaults and agent-level settings). The returned dict is merged on top of that. ###### Returns `AgentModelSettings`\[`AgentDepsT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) ##### get\_toolset ```python def get_toolset() -> AgentToolset[AgentDepsT] | None ``` Return a toolset to register with the agent, or None. ###### Returns `AgentToolset`\[`AgentDepsT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) ##### get\_native\_tools ```python def get_native_tools() -> Sequence[AgentNativeTool[AgentDepsT]] ``` Return native tools to register with the agent. ###### Returns [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`AgentNativeTool`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.AgentNativeTool)\[`AgentDepsT`\]\] ##### get\_builtin\_tools ```python def get_builtin_tools() -> Sequence[AgentNativeTool[AgentDepsT]] ``` Deprecated: use [`get_native_tools`](/docs/ai/api/pydantic-ai/capabilities/#pydantic_ai.capabilities.AbstractCapability.get_native_tools) instead. ###### Returns [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`AgentNativeTool`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.AgentNativeTool)\[`AgentDepsT`\]\] ##### get\_wrapper\_toolset ```python def get_wrapper_toolset( toolset: AbstractToolset[AgentDepsT], ) -> AbstractToolset[AgentDepsT] | None ``` Wrap the agent's assembled toolset, or return None to leave it unchanged. Called per-run with the combined non-output toolset (after the [`prepare_tools`](/docs/ai/api/pydantic-ai/capabilities/#pydantic_ai.capabilities.AbstractCapability.prepare_tools) hook has already wrapped it). Output tools are added separately and are not included. Unlike the other `get_*` methods which are called once at agent construction, this is called each run (after [`for_run`](/docs/ai/api/pydantic-ai/capabilities/#pydantic_ai.capabilities.AbstractCapability.for_run)). When multiple capabilities provide wrappers, they follow middleware semantics: the first capability in the list wraps outermost (matching `wrap_*` hooks). Use this to apply cross-cutting toolset wrappers like [`PreparedToolset`](/docs/ai/api/pydantic-ai/toolsets/#pydantic_ai.toolsets.PreparedToolset), [`FilteredToolset`](/docs/ai/api/pydantic-ai/toolsets/#pydantic_ai.toolsets.FilteredToolset), or custom [`WrapperToolset`](/docs/ai/api/pydantic-ai/toolsets/#pydantic_ai.toolsets.WrapperToolset) subclasses. ###### Returns [`AbstractToolset`](/docs/ai/api/pydantic-ai/toolsets/#pydantic_ai.toolsets.AbstractToolset)\[`AgentDepsT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) ##### prepare\_tools `@async` ```python def prepare_tools( ctx: RunContext[AgentDepsT], tool_defs: list[ToolDefinition], ) -> list[ToolDefinition] ``` Filter or modify function tool definitions for this step. Receives **function** tools only. For [output tools](/docs/ai/api/pydantic-ai/output/#pydantic_ai.output.ToolOutput), override [`prepare_output_tools`](/docs/ai/api/pydantic-ai/capabilities/#pydantic_ai.capabilities.AbstractCapability.prepare_output_tools) -- it runs separately, with `ctx.retry`/`ctx.max_retries` reflecting the **output** retry budget instead of the function-tool budget. Return a filtered or modified list. The result flows into both the model's request parameters and `ToolManager.tools`, so filtering also blocks tool execution. ###### Returns [`list`](https://docs.python.org/3/glossary.html#term-list)\[[`ToolDefinition`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.ToolDefinition)\] ##### prepare\_output\_tools `@async` ```python def prepare_output_tools( ctx: RunContext[AgentDepsT], tool_defs: list[ToolDefinition], ) -> list[ToolDefinition] ``` Filter or modify output tool definitions for this step. Receives only [output tools](/docs/ai/api/pydantic-ai/output/#pydantic_ai.output.ToolOutput). `ctx.retry` and `ctx.max_retries` reflect the **output** retry budget (agent-level `max_output_retries`), matching the output hook lifecycle. Return a filtered or modified list. The result flows into both the model's request parameters and `ToolManager.tools`, so filtering also blocks tool execution. ###### Returns [`list`](https://docs.python.org/3/glossary.html#term-list)\[[`ToolDefinition`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.ToolDefinition)\] ##### before\_run `@async` ```python def before_run(ctx: RunContext[AgentDepsT]) -> None ``` Called before the agent run starts. Observe-only; use wrap\_run for modification. ###### Returns [`None`](https://docs.python.org/3/library/constants.html#None) ##### after\_run `@async` ```python def after_run( ctx: RunContext[AgentDepsT], result: AgentRunResult[Any], ) -> AgentRunResult[Any] ``` Called after the agent run completes. Can modify the result. ###### Returns [`AgentRunResult`](/docs/ai/api/pydantic-ai/run/#pydantic_ai.run.AgentRunResult)\[[`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] ##### wrap\_run `@async` ```python def wrap_run( ctx: RunContext[AgentDepsT], handler: WrapRunHandler, ) -> AgentRunResult[Any] ``` Wraps the entire agent run. `handler()` executes the run. If `handler()` raises and this method catches the exception and returns a result instead, the error is suppressed and the recovery result is used. If this method does not call `handler()` (short-circuit), the run is skipped and the returned result is used directly. Note: if the caller cancels the run (e.g. by breaking out of an `iter()` loop), this method receives an `asyncio.CancelledError`. Implementations that hold resources should handle cleanup accordingly. ###### Returns [`AgentRunResult`](/docs/ai/api/pydantic-ai/run/#pydantic_ai.run.AgentRunResult)\[[`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] ##### on\_run\_error `@async` ```python def on_run_error( ctx: RunContext[AgentDepsT], error: BaseException, ) -> AgentRunResult[Any] ``` Called when the agent run fails with an exception. This is the error counterpart to [`after_run`](/docs/ai/api/pydantic-ai/capabilities/#pydantic_ai.capabilities.AbstractCapability.after_run): while `after_run` is called on success, `on_run_error` is called on failure (after [`wrap_run`](/docs/ai/api/pydantic-ai/capabilities/#pydantic_ai.capabilities.AbstractCapability.wrap_run) has had its chance to recover). **Raise** the original `error` (or a different exception) to propagate it. **Return** an [`AgentRunResult`](/docs/ai/api/pydantic-ai/run/#pydantic_ai.run.AgentRunResult) to suppress the error and recover the run. Not called for `GeneratorExit` or `KeyboardInterrupt`. ###### Returns [`AgentRunResult`](/docs/ai/api/pydantic-ai/run/#pydantic_ai.run.AgentRunResult)\[[`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] ##### before\_node\_run `@async` ```python def before_node_run( ctx: RunContext[AgentDepsT], node: AgentNode[AgentDepsT], ) -> AgentNode[AgentDepsT] ``` Called before each graph node executes. Can observe or replace the node. ###### Returns `AgentNode`\[`AgentDepsT`\] ##### after\_node\_run `@async` ```python def after_node_run( ctx: RunContext[AgentDepsT], node: AgentNode[AgentDepsT], result: NodeResult[AgentDepsT], ) -> NodeResult[AgentDepsT] ``` Called after each graph node succeeds. Can modify the result (next node or `End`). ###### Returns `NodeResult`\[`AgentDepsT`\] ##### wrap\_node\_run `@async` ```python def wrap_node_run( ctx: RunContext[AgentDepsT], node: AgentNode[AgentDepsT], handler: WrapNodeRunHandler[AgentDepsT], ) -> NodeResult[AgentDepsT] ``` Wraps execution of each agent graph node (run step). Called for every node in the agent graph (`UserPromptNode`, `ModelRequestNode`, `CallToolsNode`). `handler(node)` executes the node and returns the next node (or `End`). Override to inspect or modify nodes before execution, inspect or modify the returned next node, call `handler` multiple times (retry), or return a different node to redirect graph progression. Note: this hook fires when using `agent.run()`, `agent.run_stream()`, and when manually driving an [`agent.iter()`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.Agent.iter) run with `next()`, but it does **not** fire when iterating over the run with bare `async for` (which yields stream events, not node results). When using `agent.run()` with `event_stream_handler`, the handler wraps both streaming and graph advancement (i.e. the model call happens inside the wrapper). When using `agent.run_stream()`, the handler wraps only graph advancement -- streaming happens before the wrapper because `run_stream()` must yield the stream to the caller while the stream context is still open, which cannot happen from inside a callback. ###### Returns `NodeResult`\[`AgentDepsT`\] ##### on\_node\_run\_error `@async` ```python def on_node_run_error( ctx: RunContext[AgentDepsT], node: AgentNode[AgentDepsT], error: Exception, ) -> NodeResult[AgentDepsT] ``` Called when a graph node fails with an exception. This is the error counterpart to [`after_node_run`](/docs/ai/api/pydantic-ai/capabilities/#pydantic_ai.capabilities.AbstractCapability.after_node_run). **Raise** the original `error` (or a different exception) to propagate it. **Return** a next node or `End` to recover and continue the graph. Useful for recovering from [`UnexpectedModelBehavior`](/docs/ai/api/pydantic-ai/exceptions/#pydantic_ai.exceptions.UnexpectedModelBehavior) by redirecting to a different node (e.g. retry with different model settings). ###### Returns `NodeResult`\[`AgentDepsT`\] ##### wrap\_run\_event\_stream `@async` ```python def wrap_run_event_stream( ctx: RunContext[AgentDepsT], stream: AsyncIterable[AgentStreamEvent], ) -> AsyncIterable[AgentStreamEvent] ``` Wraps the event stream for a streamed node. Can observe or transform events. Note: when this method is overridden (or [`Hooks.on.event`](/docs/ai/api/pydantic-ai/capabilities/#pydantic_ai.capabilities.Hooks.on) / [`Hooks.on.run_event_stream`](/docs/ai/api/pydantic-ai/capabilities/#pydantic_ai.capabilities.Hooks.on) are registered), `agent.run()` automatically enables streaming mode so this hook fires even without an explicit `event_stream_handler`. ###### Returns [`AsyncIterable`](https://docs.python.org/3/library/typing.html#typing.AsyncIterable)\[[`AgentStreamEvent`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.AgentStreamEvent)\] ##### before\_model\_request `@async` ```python def before_model_request( ctx: RunContext[AgentDepsT], request_context: ModelRequestContext, ) -> ModelRequestContext ``` Called before each model request. Can modify messages, settings, and parameters. ###### Returns `ModelRequestContext` ##### after\_model\_request `@async` ```python def after_model_request( ctx: RunContext[AgentDepsT], request_context: ModelRequestContext, response: ModelResponse, ) -> ModelResponse ``` Called after each model response. Can modify the response before further processing. Raise [`ModelRetry`](/docs/ai/api/pydantic-ai/exceptions/#pydantic_ai.exceptions.ModelRetry) to reject the response and ask the model to try again. The original response is still appended to message history so the model can see what it said. Retries count against the output side of the agent's retry budget. ###### Returns [`ModelResponse`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.ModelResponse) ##### wrap\_model\_request `@async` ```python def wrap_model_request( ctx: RunContext[AgentDepsT], request_context: ModelRequestContext, handler: WrapModelRequestHandler, ) -> ModelResponse ``` Wraps the model request. handler() calls the model. Raise [`ModelRetry`](/docs/ai/api/pydantic-ai/exceptions/#pydantic_ai.exceptions.ModelRetry) to skip `on_model_request_error` and directly retry the model request with a retry prompt. If the handler was called, the model response is preserved in history for context (same as `after_model_request`). ###### Returns [`ModelResponse`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.ModelResponse) ##### on\_model\_request\_error `@async` ```python def on_model_request_error( ctx: RunContext[AgentDepsT], request_context: ModelRequestContext, error: Exception, ) -> ModelResponse ``` Called when a model request fails with an exception. This is the error counterpart to [`after_model_request`](/docs/ai/api/pydantic-ai/capabilities/#pydantic_ai.capabilities.AbstractCapability.after_model_request). **Raise** the original `error` (or a different exception) to propagate it. **Return** a [`ModelResponse`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.ModelResponse) to suppress the error and use the response as if the model call succeeded. **Raise** [`ModelRetry`](/docs/ai/api/pydantic-ai/exceptions/#pydantic_ai.exceptions.ModelRetry) to retry the model request with a retry prompt instead of recovering or propagating. Not called for [`SkipModelRequest`](/docs/ai/api/pydantic-ai/exceptions/#pydantic_ai.exceptions.SkipModelRequest) or [`ModelRetry`](/docs/ai/api/pydantic-ai/exceptions/#pydantic_ai.exceptions.ModelRetry). ###### Returns [`ModelResponse`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.ModelResponse) ##### before\_tool\_validate `@async` ```python def before_tool_validate( ctx: RunContext[AgentDepsT], call: ToolCallPart, tool_def: ToolDefinition, args: RawToolArgs, ) -> RawToolArgs ``` Modify raw args before validation. Raise [`ModelRetry`](/docs/ai/api/pydantic-ai/exceptions/#pydantic_ai.exceptions.ModelRetry) to skip validation and ask the model to redo the tool call. ###### Returns `RawToolArgs` ##### after\_tool\_validate `@async` ```python def after_tool_validate( ctx: RunContext[AgentDepsT], call: ToolCallPart, tool_def: ToolDefinition, args: ValidatedToolArgs, ) -> ValidatedToolArgs ``` Modify validated args. Called only on successful validation. Raise [`ModelRetry`](/docs/ai/api/pydantic-ai/exceptions/#pydantic_ai.exceptions.ModelRetry) to reject the validated args and ask the model to redo the tool call. ###### Returns `ValidatedToolArgs` ##### wrap\_tool\_validate `@async` ```python def wrap_tool_validate( ctx: RunContext[AgentDepsT], call: ToolCallPart, tool_def: ToolDefinition, args: RawToolArgs, handler: WrapToolValidateHandler, ) -> ValidatedToolArgs ``` Wraps tool argument validation. handler() runs the validation. ###### Returns `ValidatedToolArgs` ##### on\_tool\_validate\_error `@async` ```python def on_tool_validate_error( ctx: RunContext[AgentDepsT], call: ToolCallPart, tool_def: ToolDefinition, args: RawToolArgs, error: ValidationError | ModelRetry, ) -> ValidatedToolArgs ``` Called when tool argument validation fails. This is the error counterpart to [`after_tool_validate`](/docs/ai/api/pydantic-ai/capabilities/#pydantic_ai.capabilities.AbstractCapability.after_tool_validate). Fires for `ValidationError` (schema mismatch) and [`ModelRetry`](/docs/ai/api/pydantic-ai/exceptions/#pydantic_ai.exceptions.ModelRetry) (custom validator rejection). **Raise** the original `error` (or a different exception) to propagate it. **Return** validated args to suppress the error and continue as if validation passed. Not called for [`SkipToolValidation`](/docs/ai/api/pydantic-ai/exceptions/#pydantic_ai.exceptions.SkipToolValidation). ###### Returns `ValidatedToolArgs` ##### before\_tool\_execute `@async` ```python def before_tool_execute( ctx: RunContext[AgentDepsT], call: ToolCallPart, tool_def: ToolDefinition, args: ValidatedToolArgs, ) -> ValidatedToolArgs ``` Modify validated args before execution. Raise [`ModelRetry`](/docs/ai/api/pydantic-ai/exceptions/#pydantic_ai.exceptions.ModelRetry) to skip execution and ask the model to redo the tool call. ###### Returns `ValidatedToolArgs` ##### after\_tool\_execute `@async` ```python def after_tool_execute( ctx: RunContext[AgentDepsT], call: ToolCallPart, tool_def: ToolDefinition, args: ValidatedToolArgs, result: Any, ) -> Any ``` Modify result after execution. Raise [`ModelRetry`](/docs/ai/api/pydantic-ai/exceptions/#pydantic_ai.exceptions.ModelRetry) to reject the tool result and ask the model to redo the tool call. ###### Returns [`Any`](https://docs.python.org/3/library/typing.html#typing.Any) ##### wrap\_tool\_execute `@async` ```python def wrap_tool_execute( ctx: RunContext[AgentDepsT], call: ToolCallPart, tool_def: ToolDefinition, args: ValidatedToolArgs, handler: WrapToolExecuteHandler, ) -> Any ``` Wraps tool execution. handler() runs the tool. ###### Returns [`Any`](https://docs.python.org/3/library/typing.html#typing.Any) ##### on\_tool\_execute\_error `@async` ```python def on_tool_execute_error( ctx: RunContext[AgentDepsT], call: ToolCallPart, tool_def: ToolDefinition, args: ValidatedToolArgs, error: Exception, ) -> Any ``` Called when tool execution fails with an exception. This is the error counterpart to [`after_tool_execute`](/docs/ai/api/pydantic-ai/capabilities/#pydantic_ai.capabilities.AbstractCapability.after_tool_execute). **Raise** the original `error` (or a different exception) to propagate it. **Return** any value to suppress the error and use it as the tool result. **Raise** [`ModelRetry`](/docs/ai/api/pydantic-ai/exceptions/#pydantic_ai.exceptions.ModelRetry) to ask the model to redo the tool call instead of recovering or propagating. Not called for control flow exceptions ([`SkipToolExecution`](/docs/ai/api/pydantic-ai/exceptions/#pydantic_ai.exceptions.SkipToolExecution), [`CallDeferred`](/docs/ai/api/pydantic-ai/exceptions/#pydantic_ai.exceptions.CallDeferred), [`ApprovalRequired`](/docs/ai/api/pydantic-ai/exceptions/#pydantic_ai.exceptions.ApprovalRequired)) or retry signals ([`ToolRetryError`](/docs/ai/api/pydantic-ai/exceptions/#pydantic_ai.exceptions.ToolRetryError) from [`ModelRetry`](/docs/ai/api/pydantic-ai/exceptions/#pydantic_ai.exceptions.ModelRetry)). Use [`wrap_tool_execute`](/docs/ai/api/pydantic-ai/capabilities/#pydantic_ai.capabilities.AbstractCapability.wrap_tool_execute) to intercept retries. ###### Returns [`Any`](https://docs.python.org/3/library/typing.html#typing.Any) ##### before\_output\_validate `@async` ```python def before_output_validate( ctx: RunContext[AgentDepsT], output_context: OutputContext, output: RawOutput, ) -> RawOutput ``` Modify raw model output before validation/parsing. The primary hook for pre-parse repair and normalization of model output. Fires only for structured output that requires parsing: prompted, native, tool, and union output. Does **not** fire for plain text or image output. For structured text output, `output` is the raw text string from the model. For tool output, `output` is the raw tool arguments (string or dict). Raise [`ModelRetry`](/docs/ai/api/pydantic-ai/exceptions/#pydantic_ai.exceptions.ModelRetry) to skip validation and ask the model to try again with a custom message. During streaming, this hook fires on every partial validation attempt as well as the final result. Check `ctx.partial_output` to distinguish and avoid expensive work on partial results. ###### Returns `RawOutput` ##### after\_output\_validate `@async` ```python def after_output_validate( ctx: RunContext[AgentDepsT], output_context: OutputContext, output: Any, ) -> Any ``` Modify validated output after successful parsing. Called only on success. `output` is the **semantic value** the model was asked to produce -- e.g., a `MyModel` instance for `output_type=MyModel`, or `42` for `output_type=int`, or the input to a single-arg output function. For multi-arg output functions, this is the `dict` of arguments (the genuine multi-value input). Note: this differs from _tool_ hooks (`after_tool_validate`), which always see `dict[str, Any]` -- tool args follow the schema contract. Output hooks see the semantic output value, regardless of how it's internally represented during validation. Raise [`ModelRetry`](/docs/ai/api/pydantic-ai/exceptions/#pydantic_ai.exceptions.ModelRetry) to reject the validated output and ask the model to try again. ###### Returns [`Any`](https://docs.python.org/3/library/typing.html#typing.Any) ##### wrap\_output\_validate `@async` ```python def wrap_output_validate( ctx: RunContext[AgentDepsT], output_context: OutputContext, output: RawOutput, handler: WrapOutputValidateHandler, ) -> Any ``` Wraps output validation. handler(output) performs the validation. [`ModelRetry`](/docs/ai/api/pydantic-ai/exceptions/#pydantic_ai.exceptions.ModelRetry) from within the handler goes to [`on_output_validate_error`](/docs/ai/api/pydantic-ai/capabilities/#pydantic_ai.capabilities.AbstractCapability.on_output_validate_error). `ModelRetry` raised directly (not from the handler) bypasses the error hook. ###### Returns [`Any`](https://docs.python.org/3/library/typing.html#typing.Any) ##### on\_output\_validate\_error `@async` ```python def on_output_validate_error( ctx: RunContext[AgentDepsT], output_context: OutputContext, output: RawOutput, error: ValidationError | ModelRetry, ) -> Any ``` Called when output validation fails. This is the error counterpart to [`after_output_validate`](/docs/ai/api/pydantic-ai/capabilities/#pydantic_ai.capabilities.AbstractCapability.after_output_validate). **Raise** the original `error` (or a different exception) to propagate it. **Return** validated output to suppress the error and continue. ###### Returns [`Any`](https://docs.python.org/3/library/typing.html#typing.Any) ##### before\_output\_process `@async` ```python def before_output_process( ctx: RunContext[AgentDepsT], output_context: OutputContext, output: Any, ) -> Any ``` Modify validated output before processing (extraction, output function call). `output` is the **semantic value** -- e.g., a `MyModel` instance or `42`, matching `after_output_validate`. For multi-arg output functions, it's the `dict` of args. See [`after_output_validate`](/docs/ai/api/pydantic-ai/capabilities/#pydantic_ai.capabilities.AbstractCapability.after_output_validate) for a full explanation of the semantic-value contract. Raise [`ModelRetry`](/docs/ai/api/pydantic-ai/exceptions/#pydantic_ai.exceptions.ModelRetry) to skip processing and ask the model to try again. ###### Returns [`Any`](https://docs.python.org/3/library/typing.html#typing.Any) ##### after\_output\_process `@async` ```python def after_output_process( ctx: RunContext[AgentDepsT], output_context: OutputContext, output: Any, ) -> Any ``` Modify result after output processing. Raise [`ModelRetry`](/docs/ai/api/pydantic-ai/exceptions/#pydantic_ai.exceptions.ModelRetry) to reject the result and ask the model to try again. ###### Returns [`Any`](https://docs.python.org/3/library/typing.html#typing.Any) ##### wrap\_output\_process `@async` ```python def wrap_output_process( ctx: RunContext[AgentDepsT], output_context: OutputContext, output: Any, handler: WrapOutputProcessHandler, ) -> Any ``` Wraps output processing. handler(output) runs extraction + output function call. [`ModelRetry`](/docs/ai/api/pydantic-ai/exceptions/#pydantic_ai.exceptions.ModelRetry) bypasses [`on_output_process_error`](/docs/ai/api/pydantic-ai/capabilities/#pydantic_ai.capabilities.AbstractCapability.on_output_process_error) (treated as control flow, not an error). During streaming, this fires only when partial validation succeeds, and on the final result. Check `ctx.partial_output` to skip expensive work on partial results. ###### Returns [`Any`](https://docs.python.org/3/library/typing.html#typing.Any) ##### on\_output\_process\_error `@async` ```python def on_output_process_error( ctx: RunContext[AgentDepsT], output_context: OutputContext, output: Any, error: Exception, ) -> Any ``` Called when output processing fails with an exception. This is the error counterpart to [`after_output_process`](/docs/ai/api/pydantic-ai/capabilities/#pydantic_ai.capabilities.AbstractCapability.after_output_process). **Raise** the original `error` (or a different exception) to propagate it. **Return** any value to suppress the error and use it as the output. Not called for retry signals ([`ToolRetryError`](/docs/ai/api/pydantic-ai/exceptions/#pydantic_ai.exceptions.ToolRetryError) from [`ModelRetry`](/docs/ai/api/pydantic-ai/exceptions/#pydantic_ai.exceptions.ModelRetry)). ###### Returns [`Any`](https://docs.python.org/3/library/typing.html#typing.Any) ##### handle\_deferred\_tool\_calls `@async` ```python def handle_deferred_tool_calls( ctx: RunContext[AgentDepsT], requests: DeferredToolRequests, ) -> DeferredToolResults | None ``` Handle deferred tool calls (approval-required or externally-executed) inline during an agent run. Called by `ToolManager` when: - a tool raises [`ApprovalRequired`](/docs/ai/api/pydantic-ai/exceptions/#pydantic_ai.exceptions.ApprovalRequired) or [`CallDeferred`](/docs/ai/api/pydantic-ai/exceptions/#pydantic_ai.exceptions.CallDeferred) during execution, or - the model calls a tool registered with `requires_approval=True` (see [Human-in-the-Loop Tool Approval](/docs/ai/tools-toolsets/deferred-tools#human-in-the-loop-tool-approval)) or a tool backed by [external execution](/docs/ai/tools-toolsets/deferred-tools#external-tool-execution). Uses accumulation dispatch: each capability in the chain receives remaining unresolved requests and can resolve some or all of them. Results are merged and unresolved calls are passed to the next capability. **Return** a [`DeferredToolResults`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.DeferredToolResults) to resolve some or all calls. **Return** `None` to leave all calls unresolved. ###### Returns [`DeferredToolResults`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.DeferredToolResults) | [`None`](https://docs.python.org/3/library/constants.html#None) ##### prefix\_tools ```python def prefix_tools(prefix: str) -> PrefixTools[AgentDepsT] ``` Returns a new capability that wraps this one and prefixes its tool names. Only this capability's tools are prefixed; other agent tools are unaffected. ###### Returns `PrefixTools`\[`AgentDepsT`\] ### OutputContext Context about the output being processed, passed to output hooks. #### Attributes ##### mode The schema's output mode ('text', 'native', 'prompted', 'tool', 'image', 'auto'). This reflects the configured schema, not the format of this particular response. For example, a `ToolOutputSchema` with a `text_processor` (hybrid mode) reports `'tool'` even if the model returned text -- check [`tool_call`](/docs/ai/api/pydantic-ai/capabilities/#pydantic_ai.capabilities.OutputContext.tool_call) to distinguish. **Type:** `OutputMode` ##### output\_type The resolved output type (e.g. MyModel, str). For output functions, the function's input type (what the model produces). **Type:** [`type`](https://docs.python.org/3/glossary.html#term-type)\[[`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] | [`None`](https://docs.python.org/3/library/constants.html#None) ##### object\_def The output object definition (schema, name, description), if structured output. **Type:** `OutputObjectDefinition` | [`None`](https://docs.python.org/3/library/constants.html#None) ##### has\_function Whether there's an output function to call in the execute step. **Type:** [`bool`](https://docs.python.org/3/library/functions.html#bool) ##### function\_name Name of the output function that will run, when known. `None` for union processors that dispatch by output subtype, or when the schema has no function. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `None` ##### tool\_call The tool call part, for tool-based output. `None` when the current output did not arrive via a tool call (text or image). **Type:** [`ToolCallPart`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.ToolCallPart) | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `None` ##### tool\_def The tool definition, for tool-based output. `None` when the current output did not arrive via a tool call. **Type:** [`ToolDefinition`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.ToolDefinition) | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `None` ##### allows\_text Whether the schema accepts text output (including via a `text_processor` on a `ToolOutputSchema`). **Type:** [`bool`](https://docs.python.org/3/library/functions.html#bool) **Default:** `False` ##### allows\_image Whether the schema accepts image output. **Type:** [`bool`](https://docs.python.org/3/library/functions.html#bool) **Default:** `False` ##### allows\_deferred\_tools Whether the schema accepts deferred tool requests as output. **Type:** [`bool`](https://docs.python.org/3/library/functions.html#bool) **Default:** `False` ### Hooks **Bases:** `AbstractCapability[AgentDepsT]` Register hook functions via decorators or constructor kwargs. For extension developers building reusable capabilities, subclass [`AbstractCapability`](/docs/ai/api/pydantic-ai/capabilities/#pydantic_ai.capabilities.AbstractCapability) directly. For application code that needs a few hooks without the ceremony of a subclass, use `Hooks`. Example using decorators: ```python hooks = Hooks() @hooks.on.before_model_request async def log_request(ctx, request_context): print(f'Request: {request_context}') return request_context agent = Agent('openai:gpt-5', capabilities=[hooks]) ``` Example using constructor kwargs: ```python agent = Agent('openai:gpt-5', capabilities=[ Hooks(before_model_request=log_request) ]) ``` #### Attributes ##### on Decorator namespace for registering hook functions. **Type:** `_HookRegistration`\[`AgentDepsT`\] ### CapabilityFunc A sync/async function which takes a run context and returns a capability. **Type:** [`TypeAlias`](https://docs.python.org/3/library/typing.html#typing.TypeAlias) **Default:** `Callable[[RunContext[AgentDepsT]], AbstractCapability[AgentDepsT] | None | Awaitable[AbstractCapability[AgentDepsT] | None]]` ### AgentNode Type alias for an agent graph node (`UserPromptNode`, `ModelRequestNode`, `CallToolsNode`). **Type:** [`TypeAlias`](https://docs.python.org/3/library/typing.html#typing.TypeAlias) **Default:** `'_agent_graph.AgentNode[AgentDepsT, Any]'` ### NodeResult Type alias for the result of executing an agent graph node: either the next node or `End`. **Type:** [`TypeAlias`](https://docs.python.org/3/library/typing.html#typing.TypeAlias) **Default:** `'_agent_graph.AgentNode[AgentDepsT, Any] | End[FinalResult[Any]]'` ### WrapRunHandler Handler type for [`wrap_run`](/docs/ai/api/pydantic-ai/capabilities/#pydantic_ai.capabilities.AbstractCapability.wrap_run). **Type:** [`TypeAlias`](https://docs.python.org/3/library/typing.html#typing.TypeAlias) **Default:** `'Callable[[], Awaitable[AgentRunResult[Any]]]'` ### ToolSearchNativeStrategy Named provider-native tool search strategy. `'bm25'` and `'regex'` correspond to Anthropic's server-side tool search variants. OpenAI's Responses API does not expose distinct named native strategies, so these values are rejected by the OpenAI adapter. **Default:** `Literal['bm25', 'regex']` ### WrapNodeRunHandler Handler type for [`wrap_node_run`](/docs/ai/api/pydantic-ai/capabilities/#pydantic_ai.capabilities.AbstractCapability.wrap_node_run). **Type:** [`TypeAlias`](https://docs.python.org/3/library/typing.html#typing.TypeAlias) **Default:** `'Callable[[_agent_graph.AgentNode[AgentDepsT, Any]], Awaitable[_agent_graph.AgentNode[AgentDepsT, Any] | End[FinalResult[Any]]]]'` ### WrapModelRequestHandler Handler type for [`wrap_model_request`](/docs/ai/api/pydantic-ai/capabilities/#pydantic_ai.capabilities.AbstractCapability.wrap_model_request). **Type:** [`TypeAlias`](https://docs.python.org/3/library/typing.html#typing.TypeAlias) **Default:** `'Callable[[ModelRequestContext], Awaitable[ModelResponse]]'` ### RawToolArgs Type alias for raw (pre-validation) tool arguments. **Type:** [`TypeAlias`](https://docs.python.org/3/library/typing.html#typing.TypeAlias) **Default:** `str | dict[str, Any]` ### ToolSearchLocalStrategy Named local tool search strategy. `'keywords'` opts into the built-in keyword-overlap algorithm explicitly -- use this to lock in the current local algorithm rather than the `None` default (which lets Pydantic AI pick the best algorithm per provider and may change over time). Future local strategies (e.g. local BM25, TF-IDF, regex) will join this Literal as they're added; the single-member shape today is forward-compat scaffolding. **Default:** `Literal['keywords']` ### ValidatedToolArgs Type alias for validated tool arguments. **Type:** [`TypeAlias`](https://docs.python.org/3/library/typing.html#typing.TypeAlias) **Default:** `dict[str, Any]` ### WrapToolValidateHandler Handler type for [`wrap_tool_validate`](/docs/ai/api/pydantic-ai/capabilities/#pydantic_ai.capabilities.AbstractCapability.wrap_tool_validate). **Type:** [`TypeAlias`](https://docs.python.org/3/library/typing.html#typing.TypeAlias) **Default:** `Callable[[RawToolArgs], Awaitable[ValidatedToolArgs]]` ### AgentCapability A capability or a [`CapabilityFunc`](/docs/ai/api/pydantic-ai/capabilities/#pydantic_ai.capabilities.CapabilityFunc) that takes a run context and returns one. Use as the item type for `Agent(capabilities=[...])` and `agent.run(capabilities=[...])`. Functions are wrapped in a [`DynamicCapability`](/docs/ai/api/pydantic-ai/capabilities/#pydantic_ai.capabilities.DynamicCapability) automatically. **Type:** [`TypeAlias`](https://docs.python.org/3/library/typing.html#typing.TypeAlias) **Default:** `AbstractCapability[AgentDepsT] | CapabilityFunc[AgentDepsT]` ### WrapToolExecuteHandler Handler type for [`wrap_tool_execute`](/docs/ai/api/pydantic-ai/capabilities/#pydantic_ai.capabilities.AbstractCapability.wrap_tool_execute). **Type:** [`TypeAlias`](https://docs.python.org/3/library/typing.html#typing.TypeAlias) **Default:** `Callable[[ValidatedToolArgs], Awaitable[Any]]` ### ToolSearchFunc Custom search function for [`ToolSearch`](/docs/ai/api/pydantic-ai/capabilities/#pydantic_ai.capabilities.ToolSearch)'s `strategy` field. Takes the run context, the list of search queries, and the deferred tool definitions, and returns the matching tool names ordered by relevance. Both sync and async implementations are accepted. Usage `ToolSearchFunc[AgentDepsT]`. **Default:** `Callable[[RunContext[AgentDepsT], Sequence[str], Sequence['ToolDefinition']], Sequence[str] | Awaitable[Sequence[str]]]` ### RawOutput Type alias for raw output data (text or tool args). **Type:** [`TypeAlias`](https://docs.python.org/3/library/typing.html#typing.TypeAlias) **Default:** `str | dict[str, Any]` ### CAPABILITY\_TYPES Registry of all capability types that have a serialization name, mapping name to class. **Type:** [`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), [`type`](https://docs.python.org/3/glossary.html#term-type)\[`AbstractCapability`\[[`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\]\]\] **Default:** `{name: cls for cls in (NativeTool, ImageGeneration, IncludeToolReturnSchemas, Instrumentation, MCP, PrefixTools, PrepareTools, ProcessHistory, ReinjectSystemPrompt, SetToolMetadata, Thinking, ToolSearch, Toolset, WebFetch, WebSearch) if (name := (cls.get_serialization_name())) is not None}` ### WrapOutputValidateHandler Handler type for wrap\_output\_validate. **Type:** [`TypeAlias`](https://docs.python.org/3/library/typing.html#typing.TypeAlias) **Default:** `Callable[[RawOutput], Awaitable[Any]]` ### WrapOutputProcessHandler Handler type for wrap\_output\_process. **Type:** [`TypeAlias`](https://docs.python.org/3/library/typing.html#typing.TypeAlias) **Default:** `Callable[[Any], Awaitable[Any]]` ### CapabilityPosition Position tier for a capability in the middleware chain. - `'outermost'`: in the outermost tier, before all non-outermost capabilities. Multiple capabilities can declare `'outermost'`; original list order breaks ties within the tier, and `wraps`/`wrapped_by` edges refine order further. - `'innermost'`: in the innermost tier, after all non-innermost capabilities. Same tie-breaking rules apply. **Default:** `Literal['outermost', 'innermost']` ### ToolSearchStrategy Strategy value accepted by [`ToolSearch.strategy`](/docs/ai/api/pydantic-ai/capabilities/#pydantic_ai.capabilities.ToolSearch.strategy). - `'keywords'`: force the local keyword-overlap algorithm regardless of provider. - `'bm25'` / `'regex'`: force a specific provider-native strategy (Anthropic). The request fails on providers that can't honor the choice. - Callable `(ctx, queries, tools) -> names`: custom search function. Used locally, and also by the native "client-executed" surface on providers that support it (Anthropic custom tool-reference blocks, OpenAI `ToolSearchToolParam(execution='client')`). `None` is not part of the union -- it's accepted as the default on the [`ToolSearch.strategy`](/docs/ai/api/pydantic-ai/capabilities/#pydantic_ai.capabilities.ToolSearch.strategy) field and means "let Pydantic AI pick"; see that field's docstring for details. **Default:** `Union[ToolSearchFunc[AgentDepsT], ToolSearchLocalStrategy, ToolSearchNativeStrategy]` ### CapabilityRef Reference to a capability -- either a type (matches all instances of that type) or a specific instance (matches by identity). **Type:** [`TypeAlias`](https://docs.python.org/3/library/typing.html#typing.TypeAlias) **Default:** `'type[AbstractCapability[Any]] | AbstractCapability[Any]'` --- # [pydantic_ai.common_tools](https://pydantic.dev/docs/ai/api/pydantic-ai/common_tools/) # pydantic\_ai.common\_tools ### DuckDuckGoResult **Bases:** [`TypedDict`](https://docs.python.org/3/library/typing.html#typing.TypedDict) A DuckDuckGo search result. #### Attributes ##### title The title of the search result. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### href The URL of the search result. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### body The body of the search result. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ### DuckDuckGoSearchTool The DuckDuckGo search tool. #### Attributes ##### client The DuckDuckGo search client. **Type:** `DDGS` ##### max\_results The maximum number of results. If None, returns results only from the first response. **Type:** [`int`](https://docs.python.org/3/library/functions.html#int) | [`None`](https://docs.python.org/3/library/constants.html#None) #### Methods ##### \_\_call\_\_ `@async` ```python def __call__(query: str) -> list[DuckDuckGoResult] ``` Searches DuckDuckGo for the given query and returns the results. ###### Returns [`list`](https://docs.python.org/3/glossary.html#term-list)\[`DuckDuckGoResult`\] -- The search results. ###### Parameters **`query`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) The query to search for. ### duckduckgo\_search\_tool ```python def duckduckgo_search_tool( duckduckgo_client: DDGS | None = None, max_results: int | None = None, ) ``` Creates a DuckDuckGo search tool. #### Parameters **`duckduckgo_client`** : `DDGS` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` The DuckDuckGo search client. **`max_results`** : [`int`](https://docs.python.org/3/library/functions.html#int) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` The maximum number of results. If None, returns results only from the first response. Exa tools for Pydantic AI agents. Provides web search, content retrieval, and AI-powered answer capabilities using the Exa API, a neural search engine that finds high-quality, relevant results across billions of web pages. ### ExaSearchResult **Bases:** [`TypedDict`](https://docs.python.org/3/library/typing.html#typing.TypedDict) An Exa search result with content. See [Exa Search API documentation](https://docs.exa.ai/reference/search) for more information. #### Attributes ##### title The title of the search result. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### url The URL of the search result. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### published\_date The published date of the content, if available. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) ##### author The author of the content, if available. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) ##### text The text content of the search result. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ### ExaAnswerResult **Bases:** [`TypedDict`](https://docs.python.org/3/library/typing.html#typing.TypedDict) An Exa answer result with citations. See [Exa Answer API documentation](https://docs.exa.ai/reference/answer) for more information. #### Attributes ##### answer The AI-generated answer to the query. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### citations Citations supporting the answer. **Type:** [`list`](https://docs.python.org/3/glossary.html#term-list)\[[`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\]\] ### ExaContentResult **Bases:** [`TypedDict`](https://docs.python.org/3/library/typing.html#typing.TypedDict) Content retrieved from a URL. See [Exa Contents API documentation](https://docs.exa.ai/reference/get-contents) for more information. #### Attributes ##### url The URL of the content. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### title The title of the page. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### text The text content of the page. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### author The author of the content, if available. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) ##### published\_date The published date of the content, if available. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) ### ExaSearchTool The Exa search tool. #### Attributes ##### client The Exa async client. **Type:** `AsyncExa` ##### num\_results The number of results to return. **Type:** [`int`](https://docs.python.org/3/library/functions.html#int) ##### max\_characters Maximum characters of text content per result, or None for no limit. **Type:** [`int`](https://docs.python.org/3/library/functions.html#int) | [`None`](https://docs.python.org/3/library/constants.html#None) #### Methods ##### \_\_call\_\_ `@async` ```python def __call__( query: str, search_type: Literal['auto', 'keyword', 'neural', 'fast', 'deep'] = 'auto', ) -> list[ExaSearchResult] ``` Searches Exa for the given query and returns the results with content. ###### Returns [`list`](https://docs.python.org/3/glossary.html#term-list)\[`ExaSearchResult`\] -- The search results with text content. ###### Parameters **`query`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) The search query to execute with Exa. **`search_type`** : [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['auto', 'keyword', 'neural', 'fast', 'deep'\] _Default:_ `'auto'` The type of search to perform. 'auto' automatically chooses the best search type, 'keyword' for exact matches, 'neural' for semantic search, 'fast' for speed-optimized search, 'deep' for comprehensive multi-query search. ### ExaFindSimilarTool The Exa find similar tool. #### Attributes ##### client The Exa async client. **Type:** `AsyncExa` ##### num\_results The number of results to return. **Type:** [`int`](https://docs.python.org/3/library/functions.html#int) #### Methods ##### \_\_call\_\_ `@async` ```python def __call__(url: str, exclude_source_domain: bool = True) -> list[ExaSearchResult] ``` Finds pages similar to the given URL and returns them with content. ###### Returns [`list`](https://docs.python.org/3/glossary.html#term-list)\[`ExaSearchResult`\] -- Similar pages with text content. ###### Parameters **`url`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) The URL to find similar pages for. **`exclude_source_domain`** : [`bool`](https://docs.python.org/3/library/functions.html#bool) _Default:_ `True` Whether to exclude results from the same domain as the input URL. Defaults to True. ### ExaGetContentsTool The Exa get contents tool. #### Attributes ##### client The Exa async client. **Type:** `AsyncExa` #### Methods ##### \_\_call\_\_ `@async` ```python def __call__(urls: list[str]) -> list[ExaContentResult] ``` Gets the content of the specified URLs. ###### Returns [`list`](https://docs.python.org/3/glossary.html#term-list)\[`ExaContentResult`\] -- The content of each URL. ###### Parameters **`urls`** : [`list`](https://docs.python.org/3/glossary.html#term-list)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str)\] A list of URLs to get content for. ### ExaAnswerTool The Exa answer tool. #### Attributes ##### client The Exa async client. **Type:** `AsyncExa` #### Methods ##### \_\_call\_\_ `@async` ```python def __call__(query: str) -> ExaAnswerResult ``` Generates an AI-powered answer to the query with citations. ###### Returns `ExaAnswerResult` -- An answer with supporting citations from web sources. ###### Parameters **`query`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) The question to answer. ### ExaToolset **Bases:** [`FunctionToolset`](/docs/ai/api/pydantic-ai/toolsets/#pydantic_ai.toolsets.FunctionToolset) A toolset that provides Exa search tools with a shared client. This is more efficient than creating individual tools when using multiple Exa tools, as it shares a single API client across all tools. Example: ```python from pydantic_ai import Agent from pydantic_ai.common_tools.exa import ExaToolset toolset = ExaToolset(api_key='your-api-key') agent = Agent('openai:gpt-5.2', toolsets=[toolset]) ``` #### Methods ##### \_\_init\_\_ ```python def __init__( api_key: str, num_results: int = 5, max_characters: int | None = None, include_search: bool = True, include_find_similar: bool = True, include_get_contents: bool = True, include_answer: bool = True, id: str | None = None, ) ``` Creates an Exa toolset with a shared client. ###### Parameters **`api_key`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) The Exa API key. You can get one by signing up at [https://dashboard.exa.ai](https://dashboard.exa.ai). **`num_results`** : [`int`](https://docs.python.org/3/library/functions.html#int) _Default:_ `5` The number of results to return for search and find\_similar. Defaults to 5. **`max_characters`** : [`int`](https://docs.python.org/3/library/functions.html#int) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Maximum characters of text content per result. Use this to limit token usage. Defaults to None (no limit). **`include_search`** : [`bool`](https://docs.python.org/3/library/functions.html#bool) _Default:_ `True` Whether to include the search tool. Defaults to True. **`include_find_similar`** : [`bool`](https://docs.python.org/3/library/functions.html#bool) _Default:_ `True` Whether to include the find\_similar tool. Defaults to True. **`include_get_contents`** : [`bool`](https://docs.python.org/3/library/functions.html#bool) _Default:_ `True` Whether to include the get\_contents tool. Defaults to True. **`include_answer`** : [`bool`](https://docs.python.org/3/library/functions.html#bool) _Default:_ `True` Whether to include the answer tool. Defaults to True. **`id`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional ID for the toolset, used for durable execution environments. ### exa\_search\_tool ```python def exa_search_tool( api_key: str, num_results: int = 5, max_characters: int | None = None, ) -> Tool[Any] def exa_search_tool( client: AsyncExa, num_results: int = 5, max_characters: int | None = None, ) -> Tool[Any] ``` Creates an Exa search tool. #### Returns [`Tool`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.Tool)\[[`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] #### Parameters **`api_key`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` The Exa API key. Required if `client` is not provided. You can get one by signing up at [https://dashboard.exa.ai](https://dashboard.exa.ai). **`client`** : `AsyncExa` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` An existing AsyncExa client. If provided, `api_key` is ignored. This is useful for sharing a client across multiple tools. **`num_results`** : [`int`](https://docs.python.org/3/library/functions.html#int) _Default:_ `5` The number of results to return. Defaults to 5. **`max_characters`** : [`int`](https://docs.python.org/3/library/functions.html#int) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Maximum characters of text content per result. Use this to limit token usage. Defaults to None (no limit). ### exa\_find\_similar\_tool ```python def exa_find_similar_tool(api_key: str, num_results: int = 5) -> Tool[Any] def exa_find_similar_tool(client: AsyncExa, num_results: int = 5) -> Tool[Any] ``` Creates an Exa find similar tool. #### Returns [`Tool`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.Tool)\[[`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] #### Parameters **`api_key`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` The Exa API key. Required if `client` is not provided. You can get one by signing up at [https://dashboard.exa.ai](https://dashboard.exa.ai). **`client`** : `AsyncExa` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` An existing AsyncExa client. If provided, `api_key` is ignored. This is useful for sharing a client across multiple tools. **`num_results`** : [`int`](https://docs.python.org/3/library/functions.html#int) _Default:_ `5` The number of similar results to return. Defaults to 5. ### exa\_get\_contents\_tool ```python def exa_get_contents_tool(api_key: str) -> Tool[Any] def exa_get_contents_tool(client: AsyncExa) -> Tool[Any] ``` Creates an Exa get contents tool. #### Returns [`Tool`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.Tool)\[[`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] #### Parameters **`api_key`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` The Exa API key. Required if `client` is not provided. You can get one by signing up at [https://dashboard.exa.ai](https://dashboard.exa.ai). **`client`** : `AsyncExa` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` An existing AsyncExa client. If provided, `api_key` is ignored. This is useful for sharing a client across multiple tools. ### exa\_answer\_tool ```python def exa_answer_tool(api_key: str) -> Tool[Any] def exa_answer_tool(client: AsyncExa) -> Tool[Any] ``` Creates an Exa answer tool. #### Returns [`Tool`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.Tool)\[[`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] #### Parameters **`api_key`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` The Exa API key. Required if `client` is not provided. You can get one by signing up at [https://dashboard.exa.ai](https://dashboard.exa.ai). **`client`** : `AsyncExa` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` An existing AsyncExa client. If provided, `api_key` is ignored. This is useful for sharing a client across multiple tools. ### TavilySearchResult **Bases:** [`TypedDict`](https://docs.python.org/3/library/typing.html#typing.TypedDict) A Tavily search result. See [Tavily Search Endpoint documentation](https://docs.tavily.com/api-reference/endpoint/search) for more information. #### Attributes ##### title The title of the search result. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### url The URL of the search result.. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### content A short description of the search result. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### score The relevance score of the search result. **Type:** [`float`](https://docs.python.org/3/library/functions.html#float) ### TavilySearchTool The Tavily search tool. #### Attributes ##### client The Tavily search client. **Type:** `AsyncTavilyClient` ##### max\_results The maximum number of results. If None, the Tavily default is used. **Type:** [`int`](https://docs.python.org/3/library/functions.html#int) | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `None` #### Methods ##### \_\_call\_\_ `@async` ```python def __call__( query: str, search_depth: Literal['basic', 'advanced', 'fast', 'ultra-fast'] = 'basic', topic: Literal['general', 'news', 'finance'] = 'general', time_range: Literal['day', 'week', 'month', 'year'] | None = None, include_domains: list[str] | None = None, exclude_domains: list[str] | None = None, ) -> list[TavilySearchResult] ``` Searches Tavily for the given query and returns the results. ###### Returns [`list`](https://docs.python.org/3/glossary.html#term-list)\[`TavilySearchResult`\] -- A list of search results from Tavily. ###### Parameters **`query`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) The search query to execute with Tavily. **`search_depth`** : [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['basic', 'advanced', 'fast', 'ultra-fast'\] _Default:_ `'basic'` The depth of the search. **`topic`** : [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['general', 'news', 'finance'\] _Default:_ `'general'` The category of the search. **`time_range`** : [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['day', 'week', 'month', 'year'\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` The time range back from the current date to filter results. **`include_domains`** : [`list`](https://docs.python.org/3/glossary.html#term-list)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str)\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` List of domains to specifically include in the search results. **`exclude_domains`** : [`list`](https://docs.python.org/3/glossary.html#term-list)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str)\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` List of domains to specifically exclude from the search results. ### tavily\_search\_tool ```python def tavily_search_tool( api_key: str, max_results: int | None = None, search_depth: Literal['basic', 'advanced', 'fast', 'ultra-fast'] = _UNSET, topic: Literal['general', 'news', 'finance'] = _UNSET, time_range: Literal['day', 'week', 'month', 'year'] | None = _UNSET, include_domains: list[str] | None = _UNSET, exclude_domains: list[str] | None = _UNSET, ) -> Tool[Any] def tavily_search_tool( client: AsyncTavilyClient, max_results: int | None = None, search_depth: Literal['basic', 'advanced', 'fast', 'ultra-fast'] = _UNSET, topic: Literal['general', 'news', 'finance'] = _UNSET, time_range: Literal['day', 'week', 'month', 'year'] | None = _UNSET, include_domains: list[str] | None = _UNSET, exclude_domains: list[str] | None = _UNSET, ) -> Tool[Any] ``` Creates a Tavily search tool. `max_results` is always developer-controlled and does not appear in the LLM tool schema. Other parameters, when provided, are fixed for all searches and hidden from the LLM's tool schema. Parameters left unset remain available for the LLM to set per-call. #### Returns [`Tool`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.Tool)\[[`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] #### Parameters **`api_key`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` The Tavily API key. Required if `client` is not provided. You can get one by signing up at [https://app.tavily.com/home](https://app.tavily.com/home). **`client`** : `AsyncTavilyClient` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` An existing AsyncTavilyClient. If provided, `api_key` is ignored. This is useful for sharing a client across multiple tool instances. **`max_results`** : [`int`](https://docs.python.org/3/library/functions.html#int) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` The maximum number of results. If None, the Tavily default is used. **`search_depth`** : [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['basic', 'advanced', 'fast', 'ultra-fast'\] _Default:_ `_UNSET` The depth of the search. **`topic`** : [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['general', 'news', 'finance'\] _Default:_ `_UNSET` The category of the search. **`time_range`** : [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['day', 'week', 'month', 'year'\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `_UNSET` The time range back from the current date to filter results. **`include_domains`** : [`list`](https://docs.python.org/3/glossary.html#term-list)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str)\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `_UNSET` List of domains to specifically include in the search results. **`exclude_domains`** : [`list`](https://docs.python.org/3/glossary.html#term-list)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str)\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `_UNSET` List of domains to specifically exclude from the search results. --- # [pydantic_ai — Concurrency](https://pydantic.dev/docs/ai/api/pydantic-ai/concurrency/) # pydantic\_ai -- Concurrency ### ConcurrencyLimitedModel **Bases:** `WrapperModel` A model wrapper that limits concurrent requests to the underlying model. This wrapper applies concurrency limiting at the model level, ensuring that the number of concurrent requests to the model does not exceed the configured limit. This is useful for: - Respecting API rate limits - Managing resource usage - Sharing a concurrency pool across multiple models Example usage: ```python from pydantic_ai import Agent from pydantic_ai.models.concurrency import ConcurrencyLimitedModel # Limit to 5 concurrent requests model = ConcurrencyLimitedModel('openai:gpt-4o', limiter=5) agent = Agent(model) # Or share a limiter across multiple models from pydantic_ai import ConcurrencyLimiter # noqa E402 shared_limiter = ConcurrencyLimiter(max_running=10, name='openai-pool') model1 = ConcurrencyLimitedModel('openai:gpt-4o', limiter=shared_limiter) model2 = ConcurrencyLimitedModel('openai:gpt-4o-mini', limiter=shared_limiter) ``` #### Methods ##### \_\_init\_\_ ```python def __init__( wrapped: Model | KnownModelName, limiter: int | ConcurrencyLimit | AbstractConcurrencyLimiter, ) ``` Initialize the ConcurrencyLimitedModel. ###### Parameters **`wrapped`** : `Model` | `KnownModelName` The model to wrap, either a Model instance or a known model name. **`limiter`** : [`int`](https://docs.python.org/3/library/functions.html#int) | [`ConcurrencyLimit`](/docs/ai/api/pydantic-ai/concurrency/#pydantic_ai.ConcurrencyLimit) | [`AbstractConcurrencyLimiter`](/docs/ai/api/pydantic-ai/concurrency/#pydantic_ai.AbstractConcurrencyLimiter) The concurrency limit configuration. Can be: - An `int`: Simple limit on concurrent operations (unlimited queue). - A `ConcurrencyLimit`: Full configuration with optional backpressure. - An `AbstractConcurrencyLimiter`: A pre-created limiter for sharing across models. ##### request `@async` ```python def request( messages: list[ModelMessage], model_settings: ModelSettings | None, model_request_parameters: ModelRequestParameters, ) -> ModelResponse ``` Make a request to the model with concurrency limiting. ###### Returns [`ModelResponse`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.ModelResponse) ##### count\_tokens `@async` ```python def count_tokens( messages: list[ModelMessage], model_settings: ModelSettings | None, model_request_parameters: ModelRequestParameters, ) -> RequestUsage ``` Count tokens with concurrency limiting. ###### Returns [`RequestUsage`](/docs/ai/api/pydantic-ai/usage/#pydantic_ai.usage.RequestUsage) ##### request\_stream `@async` ```python def request_stream( messages: list[ModelMessage], model_settings: ModelSettings | None, model_request_parameters: ModelRequestParameters, run_context: RunContext[Any] | None = None, ) -> AsyncIterator[StreamedResponse] ``` Make a streaming request to the model with concurrency limiting. ###### Returns [`AsyncIterator`](https://docs.python.org/3/library/typing.html#typing.AsyncIterator)\[`StreamedResponse`\] ### limit\_model\_concurrency ```python def limit_model_concurrency( model: Model | KnownModelName, limiter: AnyConcurrencyLimit, ) -> Model ``` Wrap a model with concurrency limiting. This is a convenience function to wrap a model with concurrency limiting. If the limiter is None, the model is returned unchanged. Example: ```python from pydantic_ai.models.concurrency import limit_model_concurrency model = limit_model_concurrency('openai:gpt-4o', limiter=5) ``` #### Returns `Model` -- The wrapped model with concurrency limiting, or the original model if limiter is None. #### Parameters **`model`** : `Model` | `KnownModelName` The model to wrap. **`limiter`** : [`AnyConcurrencyLimit`](/docs/ai/api/pydantic-ai/concurrency/#pydantic_ai.AnyConcurrencyLimit) The concurrency limit configuration. ### AbstractConcurrencyLimiter **Bases:** `ABC` Abstract base class for concurrency limiters. Subclass this to create custom concurrency limiters (e.g., Redis-backed distributed limiters). Example: ```python from pydantic_ai.concurrency import AbstractConcurrencyLimiter class RedisConcurrencyLimiter(AbstractConcurrencyLimiter): def __init__(self, redis_client, key: str, max_running: int): self._redis = redis_client self._key = key self._max_running = max_running async def acquire(self, source: str) -> None: # Implement Redis-based distributed locking ... def release(self) -> None: # Release the Redis lock ... ``` #### Methods ##### acquire `@abstractmethod` `@async` ```python def acquire(source: str) -> None ``` Acquire a slot, waiting if necessary. ###### Returns [`None`](https://docs.python.org/3/library/constants.html#None) ###### Parameters **`source`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) Identifier for observability (e.g., 'model:gpt-4o'). ##### release `@abstractmethod` ```python def release() -> None ``` Release a slot. ###### Returns [`None`](https://docs.python.org/3/library/constants.html#None) ### ConcurrencyLimiter **Bases:** [`AbstractConcurrencyLimiter`](/docs/ai/api/pydantic-ai/concurrency/#pydantic_ai.AbstractConcurrencyLimiter) A concurrency limiter that tracks waiting operations for observability. This class wraps an anyio.CapacityLimiter and tracks the number of waiting operations. When an operation has to wait to acquire a slot, a span is created for observability purposes. #### Attributes ##### name Name of the limiter for observability. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) ##### waiting\_count Number of operations currently waiting to acquire a slot. **Type:** [`int`](https://docs.python.org/3/library/functions.html#int) ##### running\_count Number of operations currently running. **Type:** [`int`](https://docs.python.org/3/library/functions.html#int) ##### available\_count Number of slots available. **Type:** [`int`](https://docs.python.org/3/library/functions.html#int) ##### max\_running Maximum concurrent operations allowed. **Type:** [`int`](https://docs.python.org/3/library/functions.html#int) #### Methods ##### \_\_init\_\_ ```python def __init__( max_running: int, max_queued: int | None = None, name: str | None = None, tracer: Tracer | None = None, ) ``` Initialize the ConcurrencyLimiter. ###### Parameters **`max_running`** : [`int`](https://docs.python.org/3/library/functions.html#int) Maximum number of concurrent operations. **`max_queued`** : [`int`](https://docs.python.org/3/library/functions.html#int) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Maximum queue depth before raising ConcurrencyLimitExceeded. **`name`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional name for this limiter, used for observability when sharing a limiter across multiple models or agents. **`tracer`** : `Tracer` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` OpenTelemetry tracer for span creation. ##### from\_limit `@classmethod` ```python def from_limit( cls, limit: int | ConcurrencyLimit, name: str | None = None, tracer: Tracer | None = None, ) -> Self ``` Create a ConcurrencyLimiter from a ConcurrencyLimit configuration. ###### Returns [`Self`](https://docs.python.org/3/library/typing.html#typing.Self) -- A configured ConcurrencyLimiter. ###### Parameters **`limit`** : [`int`](https://docs.python.org/3/library/functions.html#int) | [`ConcurrencyLimit`](/docs/ai/api/pydantic-ai/concurrency/#pydantic_ai.ConcurrencyLimit) Either an int for simple limiting or a ConcurrencyLimit for full config. **`name`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional name for this limiter, used for observability. **`tracer`** : `Tracer` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` OpenTelemetry tracer for span creation. ##### acquire `@async` ```python def acquire(source: str) -> None ``` Acquire a slot, creating a span if waiting is required. ###### Returns [`None`](https://docs.python.org/3/library/constants.html#None) ###### Parameters **`source`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) Identifier for the source of this acquisition (e.g., 'agent:my-agent' or 'model:gpt-4'). ##### release ```python def release() -> None ``` Release a slot. ###### Returns [`None`](https://docs.python.org/3/library/constants.html#None) ### ConcurrencyLimit Configuration for concurrency limiting with optional backpressure. #### Constructor Parameters **`max_running`** : [`int`](https://docs.python.org/3/library/functions.html#int) Maximum number of concurrent operations allowed. **`max_queued`** : [`int`](https://docs.python.org/3/library/functions.html#int) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Maximum number of operations waiting in the queue. If None, the queue is unlimited. If exceeded, raises `ConcurrencyLimitExceeded`. ### AnyConcurrencyLimit Type alias for concurrency limit configuration. Can be: - An `int`: Simple limit on concurrent operations (unlimited queue). - A `ConcurrencyLimit`: Full configuration with optional backpressure. - An `AbstractConcurrencyLimiter`: A pre-created limiter instance for sharing across multiple models/agents. - `None`: No concurrency limiting (default). **Type:** [`TypeAlias`](https://docs.python.org/3/library/typing.html#typing.TypeAlias) **Default:** `'int | ConcurrencyLimit | AbstractConcurrencyLimiter | None'` ### ConcurrencyLimitExceeded **Bases:** [`AgentRunError`](/docs/ai/api/pydantic-ai/exceptions/#pydantic_ai.exceptions.AgentRunError) Error raised when the concurrency queue depth exceeds max\_queued. --- # [pydantic_ai.direct](https://pydantic.dev/docs/ai/api/pydantic-ai/direct/) # pydantic\_ai.direct Methods for making imperative requests to language models with minimal abstraction. These methods allow you to make requests to LLMs where the only abstraction is input and output schema translation so you can use all models with the same API. These methods are thin wrappers around [`Model`](/docs/ai/api/models/base/#pydantic_ai.models.Model) implementations. ### StreamedResponseSync Synchronous wrapper to async streaming responses by running the async producer in a background thread and providing a synchronous iterator. This class must be used as a context manager with the `with` statement. #### Attributes ##### response Get the current state of the response. **Type:** [`messages.ModelResponse`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.ModelResponse) ##### model\_name Get the model name of the response. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### timestamp Get the timestamp of the response. **Type:** [`datetime`](https://docs.python.org/3/library/datetime.html#module-datetime) #### Methods ##### \_\_iter\_\_ ```python def __iter__() -> Iterator[messages.ModelResponseStreamEvent] ``` Stream the response as an iterable of [`ModelResponseStreamEvent`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.ModelResponseStreamEvent)s. ###### Returns [`Iterator`](https://docs.python.org/3/library/typing.html#typing.Iterator)\[[`messages.ModelResponseStreamEvent`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.ModelResponseStreamEvent)\] ##### get ```python def get() -> messages.ModelResponse ``` Build a ModelResponse from the data received from the stream so far. ###### Returns [`messages.ModelResponse`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.ModelResponse) ##### usage ```python def usage() -> RequestUsage ``` Get the usage of the response so far. ###### Returns [`RequestUsage`](/docs/ai/api/pydantic-ai/usage/#pydantic_ai.usage.RequestUsage) ### model\_request `@async` ```python def model_request( model: models.Model | models.KnownModelName | str, messages: Sequence[messages.ModelMessage], model_settings: settings.ModelSettings | None = None, model_request_parameters: models.ModelRequestParameters | None = None, instrument: instrumented_models.InstrumentationSettings | bool | None = None, ) -> messages.ModelResponse ``` Make a non-streamed request to a model. model\_request\_example.py ```py from pydantic_ai import ModelRequest from pydantic_ai.direct import model_request async def main(): model_response = await model_request( 'anthropic:claude-haiku-4-5', [ModelRequest.user_text_prompt('What is the capital of France?')] # (1) ) print(model_response) ''' ModelResponse( parts=[TextPart(content='The capital of France is Paris.')], usage=RequestUsage(input_tokens=56, output_tokens=7), model_name='claude-haiku-4-5', timestamp=datetime.datetime(...), ) ''' ``` See [`ModelRequest.user_text_prompt`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.ModelRequest.user_text_prompt) for details. #### Returns [`messages.ModelResponse`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.ModelResponse) -- The model response and token usage associated with the request. #### Parameters **`model`** : [`models.Model`](/docs/ai/api/models/base/#pydantic_ai.models.Model) | [`models.KnownModelName`](/docs/ai/api/models/base/#pydantic_ai.models.KnownModelName) | [`str`](https://docs.python.org/3/library/stdtypes.html#str) The model to make a request to. We allow `str` here since the actual list of allowed models changes frequently. **`messages`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`messages.ModelMessage`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.ModelMessage)\] Messages to send to the model **`model_settings`** : [`settings.ModelSettings`](/docs/ai/api/pydantic-ai/settings/#pydantic_ai.settings.ModelSettings) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` optional model settings **`model_request_parameters`** : [`models.ModelRequestParameters`](/docs/ai/api/models/base/#pydantic_ai.models.ModelRequestParameters) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` optional model request parameters **`instrument`** : `instrumented_models.InstrumentationSettings` | [`bool`](https://docs.python.org/3/library/functions.html#bool) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Whether to instrument the request with OpenTelemetry/Logfire, if `None` the value from [`logfire.instrument_pydantic_ai`](https://logfire.pydantic.dev/docs/api/logfire/#logfire.Logfire.instrument_pydantic_ai) is used. ### model\_request\_sync ```python def model_request_sync( model: models.Model | models.KnownModelName | str, messages: Sequence[messages.ModelMessage], model_settings: settings.ModelSettings | None = None, model_request_parameters: models.ModelRequestParameters | None = None, instrument: instrumented_models.InstrumentationSettings | bool | None = None, ) -> messages.ModelResponse ``` Make a Synchronous, non-streamed request to a model. This is a convenience method that wraps [`model_request`](/docs/ai/api/pydantic-ai/direct/#pydantic_ai.direct.model_request) with `loop.run_until_complete(...)`. You therefore can't use this method inside async code or if there's an active event loop. model\_request\_sync\_example.py ```py from pydantic_ai import ModelRequest from pydantic_ai.direct import model_request_sync model_response = model_request_sync( 'anthropic:claude-haiku-4-5', [ModelRequest.user_text_prompt('What is the capital of France?')] # (1) ) print(model_response) ''' ModelResponse( parts=[TextPart(content='The capital of France is Paris.')], usage=RequestUsage(input_tokens=56, output_tokens=7), model_name='claude-haiku-4-5', timestamp=datetime.datetime(...), ) ''' ``` See [`ModelRequest.user_text_prompt`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.ModelRequest.user_text_prompt) for details. #### Returns [`messages.ModelResponse`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.ModelResponse) -- The model response and token usage associated with the request. #### Parameters **`model`** : [`models.Model`](/docs/ai/api/models/base/#pydantic_ai.models.Model) | [`models.KnownModelName`](/docs/ai/api/models/base/#pydantic_ai.models.KnownModelName) | [`str`](https://docs.python.org/3/library/stdtypes.html#str) The model to make a request to. We allow `str` here since the actual list of allowed models changes frequently. **`messages`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`messages.ModelMessage`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.ModelMessage)\] Messages to send to the model **`model_settings`** : [`settings.ModelSettings`](/docs/ai/api/pydantic-ai/settings/#pydantic_ai.settings.ModelSettings) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` optional model settings **`model_request_parameters`** : [`models.ModelRequestParameters`](/docs/ai/api/models/base/#pydantic_ai.models.ModelRequestParameters) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` optional model request parameters **`instrument`** : `instrumented_models.InstrumentationSettings` | [`bool`](https://docs.python.org/3/library/functions.html#bool) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Whether to instrument the request with OpenTelemetry/Logfire, if `None` the value from [`logfire.instrument_pydantic_ai`](https://logfire.pydantic.dev/docs/api/logfire/#logfire.Logfire.instrument_pydantic_ai) is used. ### model\_request\_stream ```python def model_request_stream( model: models.Model | models.KnownModelName | str, messages: Sequence[messages.ModelMessage], model_settings: settings.ModelSettings | None = None, model_request_parameters: models.ModelRequestParameters | None = None, instrument: instrumented_models.InstrumentationSettings | bool | None = None, ) -> AbstractAsyncContextManager[models.StreamedResponse] ``` Make a streamed async request to a model. model\_request\_stream\_example.py ```py from pydantic_ai import ModelRequest from pydantic_ai.direct import model_request_stream async def main(): messages = [ModelRequest.user_text_prompt('Who was Albert Einstein?')] # (1) async with model_request_stream('openai:gpt-5-mini', messages) as stream: chunks = [] async for chunk in stream: chunks.append(chunk) print(chunks) ''' [ PartStartEvent(index=0, part=TextPart(content='Albert Einstein was ')), FinalResultEvent(tool_name=None, tool_call_id=None), PartDeltaEvent( index=0, delta=TextPartDelta(content_delta='a German-born theoretical ') ), PartDeltaEvent(index=0, delta=TextPartDelta(content_delta='physicist.')), PartEndEvent( index=0, part=TextPart( content='Albert Einstein was a German-born theoretical physicist.' ), ), ] ''' ``` See [`ModelRequest.user_text_prompt`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.ModelRequest.user_text_prompt) for details. #### Returns `AbstractAsyncContextManager`\[[`models.StreamedResponse`](/docs/ai/api/models/base/#pydantic_ai.models.StreamedResponse)\] -- A [stream response](/docs/ai/api/models/base/#pydantic_ai.models.StreamedResponse) async context manager. #### Parameters **`model`** : [`models.Model`](/docs/ai/api/models/base/#pydantic_ai.models.Model) | [`models.KnownModelName`](/docs/ai/api/models/base/#pydantic_ai.models.KnownModelName) | [`str`](https://docs.python.org/3/library/stdtypes.html#str) The model to make a request to. We allow `str` here since the actual list of allowed models changes frequently. **`messages`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`messages.ModelMessage`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.ModelMessage)\] Messages to send to the model **`model_settings`** : [`settings.ModelSettings`](/docs/ai/api/pydantic-ai/settings/#pydantic_ai.settings.ModelSettings) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` optional model settings **`model_request_parameters`** : [`models.ModelRequestParameters`](/docs/ai/api/models/base/#pydantic_ai.models.ModelRequestParameters) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` optional model request parameters **`instrument`** : `instrumented_models.InstrumentationSettings` | [`bool`](https://docs.python.org/3/library/functions.html#bool) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Whether to instrument the request with OpenTelemetry/Logfire, if `None` the value from [`logfire.instrument_pydantic_ai`](https://logfire.pydantic.dev/docs/api/logfire/#logfire.Logfire.instrument_pydantic_ai) is used. ### model\_request\_stream\_sync ```python def model_request_stream_sync( model: models.Model | models.KnownModelName | str, messages: Sequence[messages.ModelMessage], model_settings: settings.ModelSettings | None = None, model_request_parameters: models.ModelRequestParameters | None = None, instrument: instrumented_models.InstrumentationSettings | bool | None = None, ) -> StreamedResponseSync ``` Make a streamed synchronous request to a model. This is the synchronous version of [`model_request_stream`](/docs/ai/api/pydantic-ai/direct/#pydantic_ai.direct.model_request_stream). It uses threading to run the asynchronous stream in the background while providing a synchronous iterator interface. model\_request\_stream\_sync\_example.py ```py from pydantic_ai import ModelRequest from pydantic_ai.direct import model_request_stream_sync messages = [ModelRequest.user_text_prompt('Who was Albert Einstein?')] with model_request_stream_sync('openai:gpt-5-mini', messages) as stream: chunks = [] for chunk in stream: chunks.append(chunk) print(chunks) ''' [ PartStartEvent(index=0, part=TextPart(content='Albert Einstein was ')), FinalResultEvent(tool_name=None, tool_call_id=None), PartDeltaEvent( index=0, delta=TextPartDelta(content_delta='a German-born theoretical ') ), PartDeltaEvent(index=0, delta=TextPartDelta(content_delta='physicist.')), PartEndEvent( index=0, part=TextPart( content='Albert Einstein was a German-born theoretical physicist.' ), ), ] ''' ``` #### Returns `StreamedResponseSync` -- A [sync stream response](/docs/ai/api/pydantic-ai/direct/#pydantic_ai.direct.StreamedResponseSync) context manager. #### Parameters **`model`** : [`models.Model`](/docs/ai/api/models/base/#pydantic_ai.models.Model) | [`models.KnownModelName`](/docs/ai/api/models/base/#pydantic_ai.models.KnownModelName) | [`str`](https://docs.python.org/3/library/stdtypes.html#str) The model to make a request to. We allow `str` here since the actual list of allowed models changes frequently. **`messages`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`messages.ModelMessage`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.ModelMessage)\] Messages to send to the model **`model_settings`** : [`settings.ModelSettings`](/docs/ai/api/pydantic-ai/settings/#pydantic_ai.settings.ModelSettings) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` optional model settings **`model_request_parameters`** : [`models.ModelRequestParameters`](/docs/ai/api/models/base/#pydantic_ai.models.ModelRequestParameters) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` optional model request parameters **`instrument`** : `instrumented_models.InstrumentationSettings` | [`bool`](https://docs.python.org/3/library/functions.html#bool) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Whether to instrument the request with OpenTelemetry/Logfire, if `None` the value from [`logfire.instrument_pydantic_ai`](https://logfire.pydantic.dev/docs/api/logfire/#logfire.Logfire.instrument_pydantic_ai) is used. --- # [pydantic_ai.durable_exec](https://pydantic.dev/docs/ai/api/pydantic-ai/durable_exec/) # pydantic\_ai.durable\_exec ### PydanticAIWorkflow Temporal Workflow base class that provides `__pydantic_ai_agents__` for direct agent registration. ### TemporalRunContext **Bases:** `RunContext[AgentDepsT]` The [`RunContext`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.RunContext) subclass to use to serialize and deserialize the run context for use inside a Temporal activity. By default, only the `deps`, `run_id`, `metadata`, `retries`, `tool_call_id`, `tool_name`, `tool_call_approved`, `tool_call_metadata`, `retry`, `max_retries`, `run_step`, `usage`, and `partial_output` attributes will be available. To make another attribute available, create a `TemporalRunContext` subclass with a custom `serialize_run_context` class method that returns a dictionary that includes the attribute and pass it to [`TemporalAgent`](/docs/ai/api/pydantic-ai/durable_exec/#pydantic_ai.durable_exec.temporal.TemporalAgent). #### Methods ##### serialize\_run\_context `@classmethod` ```python def serialize_run_context(cls, ctx: RunContext[Any]) -> dict[str, Any] ``` Serialize the run context to a `dict[str, Any]`. ###### Returns [`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] ##### deserialize\_run\_context `@classmethod` ```python def deserialize_run_context( cls, ctx: dict[str, Any], deps: Any, ) -> TemporalRunContext[Any] ``` Deserialize the run context from a `dict[str, Any]`. ###### Returns `TemporalRunContext`\[[`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] ### LogfirePlugin **Bases:** `SimplePlugin` Temporal client plugin for Logfire. ### TemporalAgent **Bases:** `WrapperAgent[AgentDepsT, OutputDataT]` #### Methods ##### \_\_init\_\_ ```python def __init__( wrapped: AbstractAgent[AgentDepsT, OutputDataT], name: str | None = None, models: Mapping[str, Model] | None = None, provider_factory: TemporalProviderFactory | None = None, event_stream_handler: EventStreamHandler[AgentDepsT] | None = None, activity_config: ActivityConfig | None = None, model_activity_config: ActivityConfig | None = None, toolset_activity_config: dict[str, ActivityConfig] | None = None, tool_activity_config: dict[str, dict[str, ActivityConfig | Literal[False]]] | None = None, run_context_type: type[TemporalRunContext[AgentDepsT]] = TemporalRunContext[AgentDepsT], temporalize_toolset_func: Callable[[AbstractToolset[AgentDepsT], str, ActivityConfig, dict[str, ActivityConfig | Literal[False]], type[AgentDepsT], type[TemporalRunContext[AgentDepsT]], AbstractAgent[AgentDepsT, Any] | None], AbstractToolset[AgentDepsT]] = temporalize_toolset, ) ``` Wrap an agent to enable it to be used inside a Temporal workflow, by automatically offloading model requests, tool calls, and MCP server communication to Temporal activities. After wrapping, the original agent can still be used as normal outside of the Temporal workflow, but any changes to its model or toolsets after wrapping will not be reflected in the durable agent. ###### Parameters **`wrapped`** : `AbstractAgent`\[`AgentDepsT`, `OutputDataT`\] The agent to wrap. **`name`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional unique agent name to use in the Temporal activities' names. If not provided, the agent's `name` will be used. **`models`** : [`Mapping`](https://docs.python.org/3/library/typing.html#typing.Mapping)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), `Model`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional mapping of model instances to register with the agent. Keys define the names that can be referenced at runtime and the values are `Model` instances. Registered model instances can be passed directly to `run(model=...)`. If the wrapped agent doesn't have a model set and none is provided to `run()`, the first model in this mapping will be used as the default. **`provider_factory`** : `TemporalProviderFactory` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional callable used when instantiating models from provider strings (those supplied at runtime). The callable receives the provider name and the current run context, allowing custom configuration such as injecting API keys stored on `deps`. Note: This factory is only used inside Temporal workflows. Outside workflows, model strings are resolved using the default provider behavior. **`event_stream_handler`** : `EventStreamHandler`\[`AgentDepsT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional event stream handler to use instead of the one set on the wrapped agent. **`activity_config`** : `ActivityConfig` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` The base Temporal activity config to use for all activities. If no config is provided, a `start_to_close_timeout` of 60 seconds is used. **`model_activity_config`** : `ActivityConfig` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` The Temporal activity config to use for model request activities. This is merged with the base activity config. **`toolset_activity_config`** : [`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), `ActivityConfig`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` The Temporal activity config to use for get-tools and call-tool activities for specific toolsets identified by ID. This is merged with the base activity config. **`tool_activity_config`** : [`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), [`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), `ActivityConfig` | [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\[[`False`](https://docs.python.org/3/library/constants.html#False)\]\]\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` The Temporal activity config to use for specific tool call activities identified by toolset ID and tool name. This is merged with the base and toolset-specific activity configs. If a tool does not use IO, you can specify `False` to disable using an activity. Note that the tool is required to be defined as an `async` function as non-async tools are run in threads which are non-deterministic and thus not supported outside of activities. **`run_context_type`** : [`type`](https://docs.python.org/3/glossary.html#term-type)\[`TemporalRunContext`\[`AgentDepsT`\]\] _Default:_ `TemporalRunContext[AgentDepsT]` The `TemporalRunContext` subclass to use to serialize and deserialize the run context for use inside a Temporal activity. By default, only the `deps`, `run_id`, `metadata`, `retries`, `tool_call_id`, `tool_name`, `tool_call_approved`, `retry`, `max_retries`, `run_step`, `usage`, and `partial_output` attributes will be available. To make another attribute available, create a `TemporalRunContext` subclass with a custom `serialize_run_context` class method that returns a dictionary that includes the attribute. **`temporalize_toolset_func`** : [`Callable`](https://docs.python.org/3/library/typing.html#typing.Callable)\[\[[`AbstractToolset`](/docs/ai/api/pydantic-ai/toolsets/#pydantic_ai.toolsets.AbstractToolset)\[`AgentDepsT`\], [`str`](https://docs.python.org/3/library/stdtypes.html#str), `ActivityConfig`, [`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), `ActivityConfig` | [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\[[`False`](https://docs.python.org/3/library/constants.html#False)\]\], [`type`](https://docs.python.org/3/glossary.html#term-type)\[`AgentDepsT`\], [`type`](https://docs.python.org/3/glossary.html#term-type)\[`TemporalRunContext`\[`AgentDepsT`\]\], `AbstractAgent`\[`AgentDepsT`, [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] | [`None`](https://docs.python.org/3/library/constants.html#None)\], [`AbstractToolset`](/docs/ai/api/pydantic-ai/toolsets/#pydantic_ai.toolsets.AbstractToolset)\[`AgentDepsT`\]\] _Default:_ `temporalize_toolset` Optional function to use to prepare "leaf" toolsets (i.e. those that implement their own tool listing and calling) for Temporal by wrapping them in a `TemporalWrapperToolset` that moves methods that require IO to Temporal activities. If not provided, only `FunctionToolset` and `MCPServer` will be prepared for Temporal. The function takes the toolset, the activity name prefix, the toolset-specific activity config, the tool-specific activity configs and the run context type. ##### run `@async` ```python def run( user_prompt: str | Sequence[_messages.UserContent] | None = None, output_type: None = None, message_history: Sequence[_messages.ModelMessage] | None = None, deferred_tool_results: DeferredToolResults | None = None, conversation_id: str | None = None, model: models.Model | models.KnownModelName | str | None = None, instructions: _instructions.AgentInstructions[AgentDepsT] = None, deps: AgentDepsT = None, model_settings: AgentModelSettings[AgentDepsT] | None = None, usage_limits: _usage.UsageLimits | None = None, usage: _usage.RunUsage | None = None, metadata: AgentMetadata[AgentDepsT] | None = None, retries: int | AgentRetries | None = None, infer_name: bool = True, toolsets: Sequence[AbstractToolset[AgentDepsT]] | None = None, event_stream_handler: EventStreamHandler[AgentDepsT] | None = None, capabilities: Sequence[AgentCapability[AgentDepsT]] | None = None, spec: dict[str, Any] | AgentSpec | None = None, ) -> AgentRunResult[OutputDataT] def run( user_prompt: str | Sequence[_messages.UserContent] | None = None, output_type: OutputSpec[RunOutputDataT], message_history: Sequence[_messages.ModelMessage] | None = None, deferred_tool_results: DeferredToolResults | None = None, conversation_id: str | None = None, model: models.Model | models.KnownModelName | str | None = None, instructions: _instructions.AgentInstructions[AgentDepsT] = None, deps: AgentDepsT = None, model_settings: AgentModelSettings[AgentDepsT] | None = None, usage_limits: _usage.UsageLimits | None = None, usage: _usage.RunUsage | None = None, metadata: AgentMetadata[AgentDepsT] | None = None, retries: int | AgentRetries | None = None, infer_name: bool = True, toolsets: Sequence[AbstractToolset[AgentDepsT]] | None = None, event_stream_handler: EventStreamHandler[AgentDepsT] | None = None, capabilities: Sequence[AgentCapability[AgentDepsT]] | None = None, spec: dict[str, Any] | AgentSpec | None = None, ) -> AgentRunResult[RunOutputDataT] ``` Run the agent with a user prompt in async mode. This method builds an internal agent graph (using system prompts, tools and result schemas) and then runs the graph to completion. The result of the run is returned. Example: ```python from pydantic_ai import Agent agent = Agent('openai:gpt-5.2') async def main(): agent_run = await agent.run('What is the capital of France?') print(agent_run.output) #> The capital of France is Paris. ``` ###### Returns [`AgentRunResult`](/docs/ai/api/pydantic-ai/run/#pydantic_ai.run.AgentRunResult)\[[`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] -- The result of the run. ###### Parameters **`user_prompt`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[`_messages.UserContent`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` User input to start/continue the conversation. **`output_type`** : `OutputSpec`\[`RunOutputDataT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Custom output type to use for this run, `output_type` may only be used if the agent has no output validators since output validators would expect an argument that matches the agent's output type. **`message_history`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[`_messages.ModelMessage`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` History of the conversation so far. **`deferred_tool_results`** : [`DeferredToolResults`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.DeferredToolResults) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional results for deferred tool calls in the message history. **`conversation_id`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` ID of the conversation this run belongs to. Pass `'new'` to start a fresh conversation, ignoring any `conversation_id` already on `message_history`. If omitted, falls back to the most recent `conversation_id` on `message_history` or a freshly generated UUID7. **`model`** : [`models.Model`](/docs/ai/api/models/base/#pydantic_ai.models.Model) | [`models.KnownModelName`](/docs/ai/api/models/base/#pydantic_ai.models.KnownModelName) | [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional model to use for this run, required if `model` was not set when creating the agent. Inside workflows, only registered model instances, registered names, or provider strings are valid. **`instructions`** : `_instructions.AgentInstructions`\[`AgentDepsT`\] _Default:_ `None` Optional additional instructions to use for this run. **`deps`** : `AgentDepsT` _Default:_ `None` Optional dependencies to use for this run. **`model_settings`** : `AgentModelSettings`\[`AgentDepsT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional settings to use for this model's request. **`usage_limits`** : `_usage.UsageLimits` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional limits on model request count or token usage. **`usage`** : `_usage.RunUsage` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional usage to start with, useful for resuming a conversation or agents used in tools. **`metadata`** : `AgentMetadata`\[`AgentDepsT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional metadata to attach to this run. Accepts a dictionary or a callable taking [`RunContext`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.RunContext); merged with the agent's configured metadata. **`retries`** : [`int`](https://docs.python.org/3/library/functions.html#int) | [`AgentRetries`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.AgentRetries) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Override the agent-level retry budgets for this run. Pass an `int` to override the output-validation budget, or an [`AgentRetries`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.AgentRetries) dict for finer control. Tool retries cannot be overridden per run. See [`Agent.__init__`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.Agent.__init__) for semantics of the two enforcement paths. **`infer_name`** : [`bool`](https://docs.python.org/3/library/functions.html#bool) _Default:_ `True` Whether to try to infer the agent name from the call frame if it's not set. **`toolsets`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`AbstractToolset`](/docs/ai/api/pydantic-ai/toolsets/#pydantic_ai.toolsets.AbstractToolset)\[`AgentDepsT`\]\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional additional toolsets for this run. **`event_stream_handler`** : `EventStreamHandler`\[`AgentDepsT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional event stream handler to use for this run. **`capabilities`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`AgentCapability`](/docs/ai/api/pydantic-ai/capabilities/#pydantic_ai.capabilities.AgentCapability)\[`AgentDepsT`\]\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional additional [capabilities](https://ai.pydantic.dev/capabilities/) for this run, merged with the agent's configured capabilities. **`spec`** : [`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] | `AgentSpec` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional agent spec to apply for this run. ##### run\_sync ```python def run_sync( user_prompt: str | Sequence[_messages.UserContent] | None = None, output_type: None = None, message_history: Sequence[_messages.ModelMessage] | None = None, deferred_tool_results: DeferredToolResults | None = None, conversation_id: str | None = None, model: models.Model | models.KnownModelName | str | None = None, instructions: _instructions.AgentInstructions[AgentDepsT] = None, deps: AgentDepsT = None, model_settings: AgentModelSettings[AgentDepsT] | None = None, usage_limits: _usage.UsageLimits | None = None, usage: _usage.RunUsage | None = None, metadata: AgentMetadata[AgentDepsT] | None = None, retries: int | AgentRetries | None = None, infer_name: bool = True, toolsets: Sequence[AbstractToolset[AgentDepsT]] | None = None, event_stream_handler: EventStreamHandler[AgentDepsT] | None = None, capabilities: Sequence[AgentCapability[AgentDepsT]] | None = None, spec: dict[str, Any] | AgentSpec | None = None, ) -> AgentRunResult[OutputDataT] def run_sync( user_prompt: str | Sequence[_messages.UserContent] | None = None, output_type: OutputSpec[RunOutputDataT], message_history: Sequence[_messages.ModelMessage] | None = None, deferred_tool_results: DeferredToolResults | None = None, conversation_id: str | None = None, model: models.Model | models.KnownModelName | str | None = None, instructions: _instructions.AgentInstructions[AgentDepsT] = None, deps: AgentDepsT = None, model_settings: AgentModelSettings[AgentDepsT] | None = None, usage_limits: _usage.UsageLimits | None = None, usage: _usage.RunUsage | None = None, metadata: AgentMetadata[AgentDepsT] | None = None, retries: int | AgentRetries | None = None, infer_name: bool = True, toolsets: Sequence[AbstractToolset[AgentDepsT]] | None = None, event_stream_handler: EventStreamHandler[AgentDepsT] | None = None, capabilities: Sequence[AgentCapability[AgentDepsT]] | None = None, spec: dict[str, Any] | AgentSpec | None = None, ) -> AgentRunResult[RunOutputDataT] ``` Synchronously run the agent with a user prompt. This is a convenience method that wraps [`self.run`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.AbstractAgent.run) with `loop.run_until_complete(...)`. You therefore can't use this method inside async code or if there's an active event loop. Example: ```python from pydantic_ai import Agent agent = Agent('openai:gpt-5.2') result_sync = agent.run_sync('What is the capital of Italy?') print(result_sync.output) #> The capital of Italy is Rome. ``` ###### Returns [`AgentRunResult`](/docs/ai/api/pydantic-ai/run/#pydantic_ai.run.AgentRunResult)\[[`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] -- The result of the run. ###### Parameters **`user_prompt`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[`_messages.UserContent`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` User input to start/continue the conversation. **`output_type`** : `OutputSpec`\[`RunOutputDataT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Custom output type to use for this run, `output_type` may only be used if the agent has no output validators since output validators would expect an argument that matches the agent's output type. **`message_history`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[`_messages.ModelMessage`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` History of the conversation so far. **`deferred_tool_results`** : [`DeferredToolResults`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.DeferredToolResults) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional results for deferred tool calls in the message history. **`conversation_id`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` ID of the conversation this run belongs to. Pass `'new'` to start a fresh conversation, ignoring any `conversation_id` already on `message_history`. If omitted, falls back to the most recent `conversation_id` on `message_history` or a freshly generated UUID7. **`model`** : [`models.Model`](/docs/ai/api/models/base/#pydantic_ai.models.Model) | [`models.KnownModelName`](/docs/ai/api/models/base/#pydantic_ai.models.KnownModelName) | [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional model to use for this run, required if `model` was not set when creating the agent. **`instructions`** : `_instructions.AgentInstructions`\[`AgentDepsT`\] _Default:_ `None` Optional additional instructions to use for this run. **`deps`** : `AgentDepsT` _Default:_ `None` Optional dependencies to use for this run. **`model_settings`** : `AgentModelSettings`\[`AgentDepsT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional settings to use for this model's request. **`usage_limits`** : `_usage.UsageLimits` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional limits on model request count or token usage. **`usage`** : `_usage.RunUsage` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional usage to start with, useful for resuming a conversation or agents used in tools. **`metadata`** : `AgentMetadata`\[`AgentDepsT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional metadata to attach to this run. Accepts a dictionary or a callable taking [`RunContext`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.RunContext); merged with the agent's configured metadata. **`retries`** : [`int`](https://docs.python.org/3/library/functions.html#int) | [`AgentRetries`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.AgentRetries) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Override the agent-level retry budgets for this run. Pass an `int` to override the output-validation budget, or an [`AgentRetries`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.AgentRetries) dict for finer control. Tool retries cannot be overridden per run. See [`Agent.__init__`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.Agent.__init__) for semantics of the two enforcement paths. **`infer_name`** : [`bool`](https://docs.python.org/3/library/functions.html#bool) _Default:_ `True` Whether to try to infer the agent name from the call frame if it's not set. **`toolsets`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`AbstractToolset`](/docs/ai/api/pydantic-ai/toolsets/#pydantic_ai.toolsets.AbstractToolset)\[`AgentDepsT`\]\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional additional toolsets for this run. **`event_stream_handler`** : `EventStreamHandler`\[`AgentDepsT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional event stream handler to use for this run. **`capabilities`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`AgentCapability`](/docs/ai/api/pydantic-ai/capabilities/#pydantic_ai.capabilities.AgentCapability)\[`AgentDepsT`\]\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional additional [capabilities](https://ai.pydantic.dev/capabilities/) for this run, merged with the agent's configured capabilities. **`spec`** : [`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] | `AgentSpec` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional agent spec to apply for this run. ##### run\_stream `@async` ```python def run_stream( user_prompt: str | Sequence[_messages.UserContent] | None = None, output_type: None = None, message_history: Sequence[_messages.ModelMessage] | None = None, deferred_tool_results: DeferredToolResults | None = None, conversation_id: str | None = None, model: models.Model | models.KnownModelName | str | None = None, instructions: _instructions.AgentInstructions[AgentDepsT] = None, deps: AgentDepsT = None, model_settings: AgentModelSettings[AgentDepsT] | None = None, usage_limits: _usage.UsageLimits | None = None, usage: _usage.RunUsage | None = None, metadata: AgentMetadata[AgentDepsT] | None = None, retries: int | AgentRetries | None = None, infer_name: bool = True, toolsets: Sequence[AbstractToolset[AgentDepsT]] | None = None, event_stream_handler: EventStreamHandler[AgentDepsT] | None = None, capabilities: Sequence[AgentCapability[AgentDepsT]] | None = None, spec: dict[str, Any] | AgentSpec | None = None, ) -> AbstractAsyncContextManager[StreamedRunResult[AgentDepsT, OutputDataT]] def run_stream( user_prompt: str | Sequence[_messages.UserContent] | None = None, output_type: OutputSpec[RunOutputDataT], message_history: Sequence[_messages.ModelMessage] | None = None, deferred_tool_results: DeferredToolResults | None = None, conversation_id: str | None = None, model: models.Model | models.KnownModelName | str | None = None, instructions: _instructions.AgentInstructions[AgentDepsT] = None, deps: AgentDepsT = None, model_settings: AgentModelSettings[AgentDepsT] | None = None, usage_limits: _usage.UsageLimits | None = None, usage: _usage.RunUsage | None = None, metadata: AgentMetadata[AgentDepsT] | None = None, retries: int | AgentRetries | None = None, infer_name: bool = True, toolsets: Sequence[AbstractToolset[AgentDepsT]] | None = None, event_stream_handler: EventStreamHandler[AgentDepsT] | None = None, capabilities: Sequence[AgentCapability[AgentDepsT]] | None = None, spec: dict[str, Any] | AgentSpec | None = None, ) -> AbstractAsyncContextManager[StreamedRunResult[AgentDepsT, RunOutputDataT]] ``` Run the agent with a user prompt in async mode, returning a streamed response. Example: ```python from pydantic_ai import Agent agent = Agent('openai:gpt-5.2') async def main(): async with agent.run_stream('What is the capital of the UK?') as response: print(await response.get_output()) #> The capital of the UK is London. ``` ###### Returns [`AsyncIterator`](https://docs.python.org/3/library/typing.html#typing.AsyncIterator)\[`StreamedRunResult`\[`AgentDepsT`, [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\]\] -- The result of the run. ###### Parameters **`user_prompt`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[`_messages.UserContent`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` User input to start/continue the conversation. **`output_type`** : `OutputSpec`\[`RunOutputDataT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Custom output type to use for this run, `output_type` may only be used if the agent has no output validators since output validators would expect an argument that matches the agent's output type. **`message_history`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[`_messages.ModelMessage`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` History of the conversation so far. **`deferred_tool_results`** : [`DeferredToolResults`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.DeferredToolResults) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional results for deferred tool calls in the message history. **`conversation_id`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` ID of the conversation this run belongs to. Pass `'new'` to start a fresh conversation, ignoring any `conversation_id` already on `message_history`. If omitted, falls back to the most recent `conversation_id` on `message_history` or a freshly generated UUID7. **`model`** : [`models.Model`](/docs/ai/api/models/base/#pydantic_ai.models.Model) | [`models.KnownModelName`](/docs/ai/api/models/base/#pydantic_ai.models.KnownModelName) | [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional model to use for this run, required if `model` was not set when creating the agent. **`instructions`** : `_instructions.AgentInstructions`\[`AgentDepsT`\] _Default:_ `None` Optional additional instructions to use for this run. **`deps`** : `AgentDepsT` _Default:_ `None` Optional dependencies to use for this run. **`model_settings`** : `AgentModelSettings`\[`AgentDepsT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional settings to use for this model's request. **`usage_limits`** : `_usage.UsageLimits` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional limits on model request count or token usage. **`usage`** : `_usage.RunUsage` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional usage to start with, useful for resuming a conversation or agents used in tools. **`metadata`** : `AgentMetadata`\[`AgentDepsT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional metadata to attach to this run. Accepts a dictionary or a callable taking [`RunContext`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.RunContext); merged with the agent's configured metadata. **`retries`** : [`int`](https://docs.python.org/3/library/functions.html#int) | [`AgentRetries`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.AgentRetries) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Override the agent-level retry budgets for this run. Pass an `int` to override the output-validation budget, or an [`AgentRetries`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.AgentRetries) dict for finer control. Tool retries cannot be overridden per run. See [`Agent.__init__`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.Agent.__init__) for semantics of the two enforcement paths. **`infer_name`** : [`bool`](https://docs.python.org/3/library/functions.html#bool) _Default:_ `True` Whether to try to infer the agent name from the call frame if it's not set. **`toolsets`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`AbstractToolset`](/docs/ai/api/pydantic-ai/toolsets/#pydantic_ai.toolsets.AbstractToolset)\[`AgentDepsT`\]\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional additional toolsets for this run. **`event_stream_handler`** : `EventStreamHandler`\[`AgentDepsT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional event stream handler to use for this run. It will receive all the events up until the final result is found, which you can then read or stream from inside the context manager. **`capabilities`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`AgentCapability`](/docs/ai/api/pydantic-ai/capabilities/#pydantic_ai.capabilities.AgentCapability)\[`AgentDepsT`\]\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional additional [capabilities](https://ai.pydantic.dev/capabilities/) for this run, merged with the agent's configured capabilities. **`spec`** : [`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] | `AgentSpec` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional agent spec to apply for this run. ##### run\_stream\_events ```python def run_stream_events( user_prompt: str | Sequence[_messages.UserContent] | None = None, output_type: None = None, message_history: Sequence[_messages.ModelMessage] | None = None, deferred_tool_results: DeferredToolResults | None = None, conversation_id: str | None = None, model: models.Model | models.KnownModelName | str | None = None, instructions: _instructions.AgentInstructions[AgentDepsT] = None, deps: AgentDepsT = None, model_settings: AgentModelSettings[AgentDepsT] | None = None, usage_limits: _usage.UsageLimits | None = None, usage: _usage.RunUsage | None = None, metadata: AgentMetadata[AgentDepsT] | None = None, retries: int | AgentRetries | None = None, infer_name: bool = True, toolsets: Sequence[AbstractToolset[AgentDepsT]] | None = None, capabilities: Sequence[AgentCapability[AgentDepsT]] | None = None, spec: dict[str, Any] | AgentSpec | None = None, ) -> AgentEventStream[OutputDataT] def run_stream_events( user_prompt: str | Sequence[_messages.UserContent] | None = None, output_type: OutputSpec[RunOutputDataT], message_history: Sequence[_messages.ModelMessage] | None = None, deferred_tool_results: DeferredToolResults | None = None, conversation_id: str | None = None, model: models.Model | models.KnownModelName | str | None = None, instructions: _instructions.AgentInstructions[AgentDepsT] = None, deps: AgentDepsT = None, model_settings: AgentModelSettings[AgentDepsT] | None = None, usage_limits: _usage.UsageLimits | None = None, usage: _usage.RunUsage | None = None, metadata: AgentMetadata[AgentDepsT] | None = None, retries: int | AgentRetries | None = None, infer_name: bool = True, toolsets: Sequence[AbstractToolset[AgentDepsT]] | None = None, capabilities: Sequence[AgentCapability[AgentDepsT]] | None = None, spec: dict[str, Any] | AgentSpec | None = None, ) -> AgentEventStream[RunOutputDataT] ``` Run the agent with a user prompt in async mode and stream events from the run. This is a convenience method that wraps [`self.run`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.AbstractAgent.run) and uses the `event_stream_handler` kwarg to get a stream of events from the run. Example: ```python from pydantic_ai import Agent, AgentRunResultEvent, AgentStreamEvent agent = Agent('openai:gpt-5.2') async def main(): events: list[AgentStreamEvent | AgentRunResultEvent] = [] async with agent.run_stream_events('What is the capital of France?') as stream: async for event in stream: events.append(event) print(events) ''' [ PartStartEvent(index=0, part=TextPart(content='The capital of ')), FinalResultEvent(tool_name=None, tool_call_id=None), PartDeltaEvent(index=0, delta=TextPartDelta(content_delta='France is Paris. ')), PartEndEvent( index=0, part=TextPart(content='The capital of France is Paris. ') ), AgentRunResultEvent( result=AgentRunResult(output='The capital of France is Paris. ') ), ] ''' ``` Arguments are the same as for [`self.run`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.AbstractAgent.run), except that `event_stream_handler` is now allowed. ###### Returns `AgentEventStream`\[[`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] -- An async iterable of stream events `AgentStreamEvent` and finally a `AgentRunResultEvent` with the final `AgentEventStream`\[[`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] -- run result. ###### Parameters **`user_prompt`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[`_messages.UserContent`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` User input to start/continue the conversation. **`output_type`** : `OutputSpec`\[`RunOutputDataT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Custom output type to use for this run, `output_type` may only be used if the agent has no output validators since output validators would expect an argument that matches the agent's output type. **`message_history`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[`_messages.ModelMessage`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` History of the conversation so far. **`deferred_tool_results`** : [`DeferredToolResults`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.DeferredToolResults) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional results for deferred tool calls in the message history. **`conversation_id`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` ID of the conversation this run belongs to. Pass `'new'` to start a fresh conversation, ignoring any `conversation_id` already on `message_history`. If omitted, falls back to the most recent `conversation_id` on `message_history` or a freshly generated UUID7. **`model`** : [`models.Model`](/docs/ai/api/models/base/#pydantic_ai.models.Model) | [`models.KnownModelName`](/docs/ai/api/models/base/#pydantic_ai.models.KnownModelName) | [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional model to use for this run, required if `model` was not set when creating the agent. **`instructions`** : `_instructions.AgentInstructions`\[`AgentDepsT`\] _Default:_ `None` Optional additional instructions to use for this run. **`deps`** : `AgentDepsT` _Default:_ `None` Optional dependencies to use for this run. **`model_settings`** : `AgentModelSettings`\[`AgentDepsT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional settings to use for this model's request. **`usage_limits`** : `_usage.UsageLimits` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional limits on model request count or token usage. **`usage`** : `_usage.RunUsage` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional usage to start with, useful for resuming a conversation or agents used in tools. **`metadata`** : `AgentMetadata`\[`AgentDepsT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional metadata to attach to this run. Accepts a dictionary or a callable taking [`RunContext`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.RunContext); merged with the agent's configured metadata. **`retries`** : [`int`](https://docs.python.org/3/library/functions.html#int) | [`AgentRetries`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.AgentRetries) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Override the agent-level retry budgets for this run. Pass an `int` to override the output-validation budget, or an [`AgentRetries`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.AgentRetries) dict for finer control. Tool retries cannot be overridden per run. See [`Agent.__init__`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.Agent.__init__) for semantics of the two enforcement paths. **`infer_name`** : [`bool`](https://docs.python.org/3/library/functions.html#bool) _Default:_ `True` Whether to try to infer the agent name from the call frame if it's not set. **`toolsets`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`AbstractToolset`](/docs/ai/api/pydantic-ai/toolsets/#pydantic_ai.toolsets.AbstractToolset)\[`AgentDepsT`\]\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional additional toolsets for this run. **`capabilities`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`AgentCapability`](/docs/ai/api/pydantic-ai/capabilities/#pydantic_ai.capabilities.AgentCapability)\[`AgentDepsT`\]\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional additional [capabilities](https://ai.pydantic.dev/capabilities/) for this run, merged with the agent's configured capabilities. **`spec`** : [`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] | `AgentSpec` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional agent spec to apply for this run. ##### iter `@async` ```python def iter( user_prompt: str | Sequence[_messages.UserContent] | None = None, output_type: None = None, message_history: Sequence[_messages.ModelMessage] | None = None, deferred_tool_results: DeferredToolResults | None = None, conversation_id: str | None = None, model: models.Model | models.KnownModelName | str | None = None, instructions: _instructions.AgentInstructions[AgentDepsT] = None, deps: AgentDepsT = None, model_settings: AgentModelSettings[AgentDepsT] | None = None, usage_limits: _usage.UsageLimits | None = None, usage: _usage.RunUsage | None = None, metadata: AgentMetadata[AgentDepsT] | None = None, retries: int | AgentRetries | None = None, infer_name: bool = True, toolsets: Sequence[AbstractToolset[AgentDepsT]] | None = None, capabilities: Sequence[AgentCapability[AgentDepsT]] | None = None, spec: dict[str, Any] | AgentSpec | None = None, _deprecated_kwargs: Any = {}, ) -> AbstractAsyncContextManager[AgentRun[AgentDepsT, OutputDataT]] def iter( user_prompt: str | Sequence[_messages.UserContent] | None = None, output_type: OutputSpec[RunOutputDataT], message_history: Sequence[_messages.ModelMessage] | None = None, deferred_tool_results: DeferredToolResults | None = None, conversation_id: str | None = None, model: models.Model | models.KnownModelName | str | None = None, instructions: _instructions.AgentInstructions[AgentDepsT] = None, deps: AgentDepsT = None, model_settings: AgentModelSettings[AgentDepsT] | None = None, usage_limits: _usage.UsageLimits | None = None, usage: _usage.RunUsage | None = None, metadata: AgentMetadata[AgentDepsT] | None = None, retries: int | AgentRetries | None = None, infer_name: bool = True, toolsets: Sequence[AbstractToolset[AgentDepsT]] | None = None, capabilities: Sequence[AgentCapability[AgentDepsT]] | None = None, spec: dict[str, Any] | AgentSpec | None = None, _deprecated_kwargs: Any = {}, ) -> AbstractAsyncContextManager[AgentRun[AgentDepsT, RunOutputDataT]] ``` A contextmanager which can be used to iterate over the agent graph's nodes as they are executed. This method builds an internal agent graph (using system prompts, tools and output schemas) and then returns an `AgentRun` object. The `AgentRun` can be used to async-iterate over the nodes of the graph as they are executed. This is the API to use if you want to consume the outputs coming from each LLM model response, or the stream of events coming from the execution of tools. The `AgentRun` also provides methods to access the full message history, new messages, and usage statistics, and the final result of the run once it has completed. For more details, see the documentation of `AgentRun`. Example: ```python from pydantic_ai import Agent agent = Agent('openai:gpt-5.2') async def main(): nodes = [] async with agent.iter('What is the capital of France?') as agent_run: async for node in agent_run: nodes.append(node) print(nodes) ''' [ UserPromptNode( user_prompt='What is the capital of France?', instructions_functions=[], system_prompts=(), system_prompt_functions=[], system_prompt_dynamic_functions={}, ), ModelRequestNode( request=ModelRequest( parts=[ UserPromptPart( content='What is the capital of France?', timestamp=datetime.datetime(...), ) ], timestamp=datetime.datetime(...), run_id='...', conversation_id='...', ) ), CallToolsNode( model_response=ModelResponse( parts=[TextPart(content='The capital of France is Paris.')], usage=RequestUsage(input_tokens=56, output_tokens=7), model_name='gpt-5.2', timestamp=datetime.datetime(...), run_id='...', conversation_id='...', ) ), End(data=FinalResult(output='The capital of France is Paris.')), ] ''' print(agent_run.result.output) #> The capital of France is Paris. ``` ###### Returns [`AsyncIterator`](https://docs.python.org/3/library/typing.html#typing.AsyncIterator)\[[`AgentRun`](/docs/ai/api/pydantic-ai/run/#pydantic_ai.run.AgentRun)\[`AgentDepsT`, [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\]\] -- The result of the run. ###### Parameters **`user_prompt`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[`_messages.UserContent`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` User input to start/continue the conversation. **`output_type`** : `OutputSpec`\[`RunOutputDataT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Custom output type to use for this run, `output_type` may only be used if the agent has no output validators since output validators would expect an argument that matches the agent's output type. **`message_history`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[`_messages.ModelMessage`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` History of the conversation so far. **`deferred_tool_results`** : [`DeferredToolResults`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.DeferredToolResults) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional results for deferred tool calls in the message history. **`conversation_id`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` ID of the conversation this run belongs to. Pass `'new'` to start a fresh conversation, ignoring any `conversation_id` already on `message_history`. If omitted, falls back to the most recent `conversation_id` on `message_history` or a freshly generated UUID7. **`model`** : [`models.Model`](/docs/ai/api/models/base/#pydantic_ai.models.Model) | [`models.KnownModelName`](/docs/ai/api/models/base/#pydantic_ai.models.KnownModelName) | [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional model to use for this run, required if `model` was not set when creating the agent. **`instructions`** : `_instructions.AgentInstructions`\[`AgentDepsT`\] _Default:_ `None` Optional additional instructions to use for this run. **`deps`** : `AgentDepsT` _Default:_ `None` Optional dependencies to use for this run. **`model_settings`** : `AgentModelSettings`\[`AgentDepsT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional settings to use for this model's request. **`usage_limits`** : `_usage.UsageLimits` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional limits on model request count or token usage. **`usage`** : `_usage.RunUsage` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional usage to start with, useful for resuming a conversation or agents used in tools. **`metadata`** : `AgentMetadata`\[`AgentDepsT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional metadata to attach to this run. Accepts a dictionary or a callable taking [`RunContext`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.RunContext); merged with the agent's configured metadata. **`retries`** : [`int`](https://docs.python.org/3/library/functions.html#int) | [`AgentRetries`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.AgentRetries) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Override the agent-level retry budgets for this run. Pass an `int` to override the output-validation budget, or an [`AgentRetries`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.AgentRetries) dict for finer control. Tool retries cannot be overridden per run. See [`Agent.__init__`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.Agent.__init__) for semantics of the two enforcement paths. **`infer_name`** : [`bool`](https://docs.python.org/3/library/functions.html#bool) _Default:_ `True` Whether to try to infer the agent name from the call frame if it's not set. **`toolsets`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`AbstractToolset`](/docs/ai/api/pydantic-ai/toolsets/#pydantic_ai.toolsets.AbstractToolset)\[`AgentDepsT`\]\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional additional toolsets for this run. **`capabilities`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`AgentCapability`](/docs/ai/api/pydantic-ai/capabilities/#pydantic_ai.capabilities.AgentCapability)\[`AgentDepsT`\]\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional additional [capabilities](https://ai.pydantic.dev/capabilities/) for this run, merged with the agent's configured capabilities. **`spec`** : [`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] | `AgentSpec` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional agent spec to apply for this run. ##### override ```python def override( name: str | _utils.Unset = _utils.UNSET, deps: AgentDepsT | _utils.Unset = _utils.UNSET, model: models.Model | models.KnownModelName | str | _utils.Unset = _utils.UNSET, toolsets: Sequence[AbstractToolset[AgentDepsT]] | _utils.Unset = _utils.UNSET, tools: Sequence[Tool[AgentDepsT] | ToolFuncEither[AgentDepsT, ...]] | _utils.Unset = _utils.UNSET, native_tools: Sequence[AgentNativeTool[AgentDepsT]] | _utils.Unset = _utils.UNSET, instructions: _instructions.AgentInstructions[AgentDepsT] | _utils.Unset = _utils.UNSET, model_settings: AgentModelSettings[AgentDepsT] | _utils.Unset = _utils.UNSET, retries: int | AgentRetries | _utils.Unset = _utils.UNSET, spec: dict[str, Any] | AgentSpec | None = None, _deprecated_kwargs: Any = {}, ) -> Iterator[None] ``` Context manager to temporarily override agent configuration. This is particularly useful when testing. You can find an example of this [here](/docs/ai/guides/testing#overriding-model-via-pytest-fixtures). ###### Returns [`Iterator`](https://docs.python.org/3/library/typing.html#typing.Iterator)\[[`None`](https://docs.python.org/3/library/constants.html#None)\] ###### Parameters **`name`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | `_utils.Unset` _Default:_ `_utils.UNSET` The name to use instead of the name passed to the agent constructor and agent run. **`deps`** : `AgentDepsT` | `_utils.Unset` _Default:_ `_utils.UNSET` The dependencies to use instead of the dependencies passed to the agent run. **`model`** : [`models.Model`](/docs/ai/api/models/base/#pydantic_ai.models.Model) | [`models.KnownModelName`](/docs/ai/api/models/base/#pydantic_ai.models.KnownModelName) | [`str`](https://docs.python.org/3/library/stdtypes.html#str) | `_utils.Unset` _Default:_ `_utils.UNSET` The model to use instead of the model passed to the agent run. **`toolsets`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`AbstractToolset`](/docs/ai/api/pydantic-ai/toolsets/#pydantic_ai.toolsets.AbstractToolset)\[`AgentDepsT`\]\] | `_utils.Unset` _Default:_ `_utils.UNSET` The toolsets to use instead of the toolsets passed to the agent constructor and agent run. **`tools`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`Tool`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.Tool)\[`AgentDepsT`\] | `ToolFuncEither`\[`AgentDepsT`, ...\]\] | `_utils.Unset` _Default:_ `_utils.UNSET` The tools to use instead of the tools registered with the agent. **`native_tools`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`AgentNativeTool`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.AgentNativeTool)\[`AgentDepsT`\]\] | `_utils.Unset` _Default:_ `_utils.UNSET` The native tools to use instead of the agent's configured native tools. **`instructions`** : `_instructions.AgentInstructions`\[`AgentDepsT`\] | `_utils.Unset` _Default:_ `_utils.UNSET` The instructions to use instead of the instructions registered with the agent. **`model_settings`** : `AgentModelSettings`\[`AgentDepsT`\] | `_utils.Unset` _Default:_ `_utils.UNSET` The model settings to use instead of the model settings passed to the agent constructor. When set, any per-run `model_settings` argument is ignored. **`retries`** : [`int`](https://docs.python.org/3/library/functions.html#int) | [`AgentRetries`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.AgentRetries) | `_utils.Unset` _Default:_ `_utils.UNSET` The retry budgets to use instead of the agent-level configuration. Pass an `int` to override the output-validation budget, or an [`AgentRetries`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.AgentRetries) dict for finer control. When set, any per-run `retries` argument is ignored. **`spec`** : [`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] | `AgentSpec` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional agent spec to apply as overrides. ### TemporalWrapperToolset **Bases:** `WrapperToolset[AgentDepsT]`, `ABC` ### PydanticAIPlugin **Bases:** `SimplePlugin` Temporal client and worker plugin for Pydantic AI. ### AgentPlugin **Bases:** `SimplePlugin` Temporal worker plugin for a specific Pydantic AI agent. ### StepConfig **Bases:** [`TypedDict`](https://docs.python.org/3/library/typing.html#typing.TypedDict) Configuration for a step in the DBOS workflow. ### DBOSMCPServer **Bases:** `DBOSMCPToolsetBase[AgentDepsT]` A wrapper for MCPServer that integrates with DBOS, turning call\_tool and get\_tools into DBOS steps. Tool definitions are cached across steps to avoid redundant MCP server round-trips, respecting the wrapped server's `cache_tools` setting. ### DBOSModel **Bases:** `WrapperModel` A wrapper for Model that integrates with DBOS, turning request and request\_stream to DBOS steps. ### DBOSAgent **Bases:** `WrapperAgent[AgentDepsT, OutputDataT]`, `DBOSConfiguredInstance` #### Methods ##### \_\_init\_\_ ```python def __init__( wrapped: AbstractAgent[AgentDepsT, OutputDataT], name: str | None = None, event_stream_handler: EventStreamHandler[AgentDepsT] | None = None, mcp_step_config: StepConfig | None = None, model_step_config: StepConfig | None = None, parallel_execution_mode: DBOSParallelExecutionMode = 'parallel_ordered_events', ) ``` Wrap an agent to enable it with DBOS durable workflows, by automatically offloading model requests, tool calls, and MCP server communication to DBOS steps. After wrapping, the original agent can still be used as normal outside of the DBOS workflow. ###### Parameters **`wrapped`** : `AbstractAgent`\[`AgentDepsT`, `OutputDataT`\] The agent to wrap. **`name`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional unique agent name to use as the DBOS configured instance name. If not provided, the agent's `name` will be used. **`event_stream_handler`** : `EventStreamHandler`\[`AgentDepsT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional event stream handler to use instead of the one set on the wrapped agent. **`mcp_step_config`** : `StepConfig` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` The base DBOS step config to use for MCP server steps. If no config is provided, use the default settings of DBOS. **`model_step_config`** : `StepConfig` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` The DBOS step config to use for model request steps. If no config is provided, use the default settings of DBOS. **`parallel_execution_mode`** : `DBOSParallelExecutionMode` _Default:_ `'parallel_ordered_events'` The mode for executing tool calls: - 'parallel\_ordered\_events' (default): Run tool calls in parallel, but events are emitted in order, after all calls complete. - 'sequential': Run tool calls one at a time in order. ##### run `@async` ```python def run( user_prompt: str | Sequence[_messages.UserContent] | None = None, output_type: None = None, message_history: Sequence[_messages.ModelMessage] | None = None, deferred_tool_results: DeferredToolResults | None = None, conversation_id: str | None = None, model: models.Model | models.KnownModelName | str | None = None, instructions: _instructions.AgentInstructions[AgentDepsT] = None, deps: AgentDepsT = None, model_settings: AgentModelSettings[AgentDepsT] | None = None, usage_limits: _usage.UsageLimits | None = None, usage: _usage.RunUsage | None = None, metadata: AgentMetadata[AgentDepsT] | None = None, retries: int | AgentRetries | None = None, infer_name: bool = True, toolsets: Sequence[AbstractToolset[AgentDepsT]] | None = None, event_stream_handler: EventStreamHandler[AgentDepsT] | None = None, capabilities: Sequence[AgentCapability[AgentDepsT]] | None = None, spec: dict[str, Any] | AgentSpec | None = None, ) -> AgentRunResult[OutputDataT] def run( user_prompt: str | Sequence[_messages.UserContent] | None = None, output_type: OutputSpec[RunOutputDataT], message_history: Sequence[_messages.ModelMessage] | None = None, deferred_tool_results: DeferredToolResults | None = None, conversation_id: str | None = None, model: models.Model | models.KnownModelName | str | None = None, instructions: _instructions.AgentInstructions[AgentDepsT] = None, deps: AgentDepsT = None, model_settings: AgentModelSettings[AgentDepsT] | None = None, usage_limits: _usage.UsageLimits | None = None, usage: _usage.RunUsage | None = None, metadata: AgentMetadata[AgentDepsT] | None = None, retries: int | AgentRetries | None = None, infer_name: bool = True, toolsets: Sequence[AbstractToolset[AgentDepsT]] | None = None, event_stream_handler: EventStreamHandler[AgentDepsT] | None = None, capabilities: Sequence[AgentCapability[AgentDepsT]] | None = None, spec: dict[str, Any] | AgentSpec | None = None, ) -> AgentRunResult[RunOutputDataT] ``` Run the agent with a user prompt in async mode. This method builds an internal agent graph (using system prompts, tools and result schemas) and then runs the graph to completion. The result of the run is returned. Example: ```python from pydantic_ai import Agent agent = Agent('openai:gpt-5.2') async def main(): agent_run = await agent.run('What is the capital of France?') print(agent_run.output) #> The capital of France is Paris. ``` ###### Returns [`AgentRunResult`](/docs/ai/api/pydantic-ai/run/#pydantic_ai.run.AgentRunResult)\[[`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] -- The result of the run. ###### Parameters **`user_prompt`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[`_messages.UserContent`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` User input to start/continue the conversation. **`output_type`** : `OutputSpec`\[`RunOutputDataT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Custom output type to use for this run, `output_type` may only be used if the agent has no output validators since output validators would expect an argument that matches the agent's output type. **`message_history`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[`_messages.ModelMessage`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` History of the conversation so far. **`deferred_tool_results`** : [`DeferredToolResults`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.DeferredToolResults) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional results for deferred tool calls in the message history. **`conversation_id`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` ID of the conversation this run belongs to. Pass `'new'` to start a fresh conversation, ignoring any `conversation_id` already on `message_history`. If omitted, falls back to the most recent `conversation_id` on `message_history` or a freshly generated UUID7. **`model`** : [`models.Model`](/docs/ai/api/models/base/#pydantic_ai.models.Model) | [`models.KnownModelName`](/docs/ai/api/models/base/#pydantic_ai.models.KnownModelName) | [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional model to use for this run, required if `model` was not set when creating the agent. **`instructions`** : `_instructions.AgentInstructions`\[`AgentDepsT`\] _Default:_ `None` Optional additional instructions to use for this run. **`deps`** : `AgentDepsT` _Default:_ `None` Optional dependencies to use for this run. **`model_settings`** : `AgentModelSettings`\[`AgentDepsT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional settings to use for this model's request. **`usage_limits`** : `_usage.UsageLimits` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional limits on model request count or token usage. **`usage`** : `_usage.RunUsage` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional usage to start with, useful for resuming a conversation or agents used in tools. **`metadata`** : `AgentMetadata`\[`AgentDepsT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional metadata to attach to this run. Accepts a dictionary or a callable taking [`RunContext`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.RunContext); merged with the agent's configured metadata. **`retries`** : [`int`](https://docs.python.org/3/library/functions.html#int) | [`AgentRetries`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.AgentRetries) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Override the agent-level retry budgets for this run. Pass an `int` to override the output-validation budget, or an [`AgentRetries`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.AgentRetries) dict for finer control. Tool retries cannot be overridden per run. See [`Agent.__init__`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.Agent.__init__) for semantics of the two enforcement paths. **`infer_name`** : [`bool`](https://docs.python.org/3/library/functions.html#bool) _Default:_ `True` Whether to try to infer the agent name from the call frame if it's not set. **`toolsets`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`AbstractToolset`](/docs/ai/api/pydantic-ai/toolsets/#pydantic_ai.toolsets.AbstractToolset)\[`AgentDepsT`\]\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional additional toolsets for this run. **`event_stream_handler`** : `EventStreamHandler`\[`AgentDepsT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional event stream handler to use for this run. **`capabilities`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`AgentCapability`](/docs/ai/api/pydantic-ai/capabilities/#pydantic_ai.capabilities.AgentCapability)\[`AgentDepsT`\]\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional additional [capabilities](https://ai.pydantic.dev/capabilities/) for this run, merged with the agent's configured capabilities. **`spec`** : [`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] | `AgentSpec` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional agent spec to apply for this run. ##### run\_sync ```python def run_sync( user_prompt: str | Sequence[_messages.UserContent] | None = None, output_type: None = None, message_history: Sequence[_messages.ModelMessage] | None = None, deferred_tool_results: DeferredToolResults | None = None, conversation_id: str | None = None, model: models.Model | models.KnownModelName | str | None = None, instructions: _instructions.AgentInstructions[AgentDepsT] = None, deps: AgentDepsT = None, model_settings: AgentModelSettings[AgentDepsT] | None = None, usage_limits: _usage.UsageLimits | None = None, usage: _usage.RunUsage | None = None, metadata: AgentMetadata[AgentDepsT] | None = None, retries: int | AgentRetries | None = None, infer_name: bool = True, toolsets: Sequence[AbstractToolset[AgentDepsT]] | None = None, event_stream_handler: EventStreamHandler[AgentDepsT] | None = None, capabilities: Sequence[AgentCapability[AgentDepsT]] | None = None, spec: dict[str, Any] | AgentSpec | None = None, ) -> AgentRunResult[OutputDataT] def run_sync( user_prompt: str | Sequence[_messages.UserContent] | None = None, output_type: OutputSpec[RunOutputDataT], message_history: Sequence[_messages.ModelMessage] | None = None, deferred_tool_results: DeferredToolResults | None = None, conversation_id: str | None = None, model: models.Model | models.KnownModelName | str | None = None, instructions: _instructions.AgentInstructions[AgentDepsT] = None, deps: AgentDepsT = None, model_settings: AgentModelSettings[AgentDepsT] | None = None, usage_limits: _usage.UsageLimits | None = None, usage: _usage.RunUsage | None = None, metadata: AgentMetadata[AgentDepsT] | None = None, retries: int | AgentRetries | None = None, infer_name: bool = True, toolsets: Sequence[AbstractToolset[AgentDepsT]] | None = None, event_stream_handler: EventStreamHandler[AgentDepsT] | None = None, capabilities: Sequence[AgentCapability[AgentDepsT]] | None = None, spec: dict[str, Any] | AgentSpec | None = None, ) -> AgentRunResult[RunOutputDataT] ``` Synchronously run the agent with a user prompt. This is a convenience method that wraps [`self.run`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.AbstractAgent.run) with `loop.run_until_complete(...)`. You therefore can't use this method inside async code or if there's an active event loop. Example: ```python from pydantic_ai import Agent agent = Agent('openai:gpt-5.2') result_sync = agent.run_sync('What is the capital of Italy?') print(result_sync.output) #> The capital of Italy is Rome. ``` ###### Returns [`AgentRunResult`](/docs/ai/api/pydantic-ai/run/#pydantic_ai.run.AgentRunResult)\[[`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] -- The result of the run. ###### Parameters **`user_prompt`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[`_messages.UserContent`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` User input to start/continue the conversation. **`output_type`** : `OutputSpec`\[`RunOutputDataT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Custom output type to use for this run, `output_type` may only be used if the agent has no output validators since output validators would expect an argument that matches the agent's output type. **`message_history`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[`_messages.ModelMessage`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` History of the conversation so far. **`deferred_tool_results`** : [`DeferredToolResults`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.DeferredToolResults) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional results for deferred tool calls in the message history. **`conversation_id`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` ID of the conversation this run belongs to. Pass `'new'` to start a fresh conversation, ignoring any `conversation_id` already on `message_history`. If omitted, falls back to the most recent `conversation_id` on `message_history` or a freshly generated UUID7. **`model`** : [`models.Model`](/docs/ai/api/models/base/#pydantic_ai.models.Model) | [`models.KnownModelName`](/docs/ai/api/models/base/#pydantic_ai.models.KnownModelName) | [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional model to use for this run, required if `model` was not set when creating the agent. **`instructions`** : `_instructions.AgentInstructions`\[`AgentDepsT`\] _Default:_ `None` Optional additional instructions to use for this run. **`deps`** : `AgentDepsT` _Default:_ `None` Optional dependencies to use for this run. **`model_settings`** : `AgentModelSettings`\[`AgentDepsT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional settings to use for this model's request. **`usage_limits`** : `_usage.UsageLimits` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional limits on model request count or token usage. **`usage`** : `_usage.RunUsage` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional usage to start with, useful for resuming a conversation or agents used in tools. **`metadata`** : `AgentMetadata`\[`AgentDepsT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional metadata to attach to this run. Accepts a dictionary or a callable taking [`RunContext`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.RunContext); merged with the agent's configured metadata. **`retries`** : [`int`](https://docs.python.org/3/library/functions.html#int) | [`AgentRetries`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.AgentRetries) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Override the agent-level retry budgets for this run. Pass an `int` to override the output-validation budget, or an [`AgentRetries`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.AgentRetries) dict for finer control. Tool retries cannot be overridden per run. See [`Agent.__init__`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.Agent.__init__) for semantics of the two enforcement paths. **`infer_name`** : [`bool`](https://docs.python.org/3/library/functions.html#bool) _Default:_ `True` Whether to try to infer the agent name from the call frame if it's not set. **`toolsets`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`AbstractToolset`](/docs/ai/api/pydantic-ai/toolsets/#pydantic_ai.toolsets.AbstractToolset)\[`AgentDepsT`\]\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional additional toolsets for this run. **`event_stream_handler`** : `EventStreamHandler`\[`AgentDepsT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional event stream handler to use for this run. **`capabilities`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`AgentCapability`](/docs/ai/api/pydantic-ai/capabilities/#pydantic_ai.capabilities.AgentCapability)\[`AgentDepsT`\]\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional additional [capabilities](https://ai.pydantic.dev/capabilities/) for this run, merged with the agent's configured capabilities. **`spec`** : [`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] | `AgentSpec` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional agent spec to apply for this run. ##### run\_stream `@async` ```python def run_stream( user_prompt: str | Sequence[_messages.UserContent] | None = None, output_type: None = None, message_history: Sequence[_messages.ModelMessage] | None = None, deferred_tool_results: DeferredToolResults | None = None, conversation_id: str | None = None, model: models.Model | models.KnownModelName | str | None = None, instructions: _instructions.AgentInstructions[AgentDepsT] = None, deps: AgentDepsT = None, model_settings: AgentModelSettings[AgentDepsT] | None = None, usage_limits: _usage.UsageLimits | None = None, usage: _usage.RunUsage | None = None, metadata: AgentMetadata[AgentDepsT] | None = None, retries: int | AgentRetries | None = None, infer_name: bool = True, toolsets: Sequence[AbstractToolset[AgentDepsT]] | None = None, event_stream_handler: EventStreamHandler[AgentDepsT] | None = None, capabilities: Sequence[AgentCapability[AgentDepsT]] | None = None, spec: dict[str, Any] | AgentSpec | None = None, ) -> AbstractAsyncContextManager[StreamedRunResult[AgentDepsT, OutputDataT]] def run_stream( user_prompt: str | Sequence[_messages.UserContent] | None = None, output_type: OutputSpec[RunOutputDataT], message_history: Sequence[_messages.ModelMessage] | None = None, deferred_tool_results: DeferredToolResults | None = None, conversation_id: str | None = None, model: models.Model | models.KnownModelName | str | None = None, deps: AgentDepsT = None, instructions: _instructions.AgentInstructions[AgentDepsT] = None, model_settings: AgentModelSettings[AgentDepsT] | None = None, usage_limits: _usage.UsageLimits | None = None, usage: _usage.RunUsage | None = None, metadata: AgentMetadata[AgentDepsT] | None = None, retries: int | AgentRetries | None = None, infer_name: bool = True, toolsets: Sequence[AbstractToolset[AgentDepsT]] | None = None, event_stream_handler: EventStreamHandler[AgentDepsT] | None = None, capabilities: Sequence[AgentCapability[AgentDepsT]] | None = None, spec: dict[str, Any] | AgentSpec | None = None, ) -> AbstractAsyncContextManager[StreamedRunResult[AgentDepsT, RunOutputDataT]] ``` Run the agent with a user prompt in async mode, returning a streamed response. Example: ```python from pydantic_ai import Agent agent = Agent('openai:gpt-5.2') async def main(): async with agent.run_stream('What is the capital of the UK?') as response: print(await response.get_output()) #> The capital of the UK is London. ``` ###### Returns [`AsyncIterator`](https://docs.python.org/3/library/typing.html#typing.AsyncIterator)\[`StreamedRunResult`\[`AgentDepsT`, [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\]\] -- The result of the run. ###### Parameters **`user_prompt`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[`_messages.UserContent`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` User input to start/continue the conversation. **`output_type`** : `OutputSpec`\[`RunOutputDataT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Custom output type to use for this run, `output_type` may only be used if the agent has no output validators since output validators would expect an argument that matches the agent's output type. **`message_history`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[`_messages.ModelMessage`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` History of the conversation so far. **`deferred_tool_results`** : [`DeferredToolResults`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.DeferredToolResults) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional results for deferred tool calls in the message history. **`conversation_id`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` ID of the conversation this run belongs to. Pass `'new'` to start a fresh conversation, ignoring any `conversation_id` already on `message_history`. If omitted, falls back to the most recent `conversation_id` on `message_history` or a freshly generated UUID7. **`model`** : [`models.Model`](/docs/ai/api/models/base/#pydantic_ai.models.Model) | [`models.KnownModelName`](/docs/ai/api/models/base/#pydantic_ai.models.KnownModelName) | [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional model to use for this run, required if `model` was not set when creating the agent. **`instructions`** : `_instructions.AgentInstructions`\[`AgentDepsT`\] _Default:_ `None` Optional additional instructions to use for this run. **`deps`** : `AgentDepsT` _Default:_ `None` Optional dependencies to use for this run. **`model_settings`** : `AgentModelSettings`\[`AgentDepsT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional settings to use for this model's request. **`usage_limits`** : `_usage.UsageLimits` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional limits on model request count or token usage. **`usage`** : `_usage.RunUsage` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional usage to start with, useful for resuming a conversation or agents used in tools. **`metadata`** : `AgentMetadata`\[`AgentDepsT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional metadata to attach to this run. Accepts a dictionary or a callable taking [`RunContext`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.RunContext); merged with the agent's configured metadata. **`retries`** : [`int`](https://docs.python.org/3/library/functions.html#int) | [`AgentRetries`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.AgentRetries) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Override the agent-level retry budgets for this run. Pass an `int` to override the output-validation budget, or an [`AgentRetries`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.AgentRetries) dict for finer control. Tool retries cannot be overridden per run. See [`Agent.__init__`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.Agent.__init__) for semantics of the two enforcement paths. **`infer_name`** : [`bool`](https://docs.python.org/3/library/functions.html#bool) _Default:_ `True` Whether to try to infer the agent name from the call frame if it's not set. **`toolsets`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`AbstractToolset`](/docs/ai/api/pydantic-ai/toolsets/#pydantic_ai.toolsets.AbstractToolset)\[`AgentDepsT`\]\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional additional toolsets for this run. **`event_stream_handler`** : `EventStreamHandler`\[`AgentDepsT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional event stream handler to use for this run. It will receive all the events up until the final result is found, which you can then read or stream from inside the context manager. **`capabilities`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`AgentCapability`](/docs/ai/api/pydantic-ai/capabilities/#pydantic_ai.capabilities.AgentCapability)\[`AgentDepsT`\]\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional additional [capabilities](https://ai.pydantic.dev/capabilities/) for this run, merged with the agent's configured capabilities. **`spec`** : [`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] | `AgentSpec` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional agent spec to apply for this run. ##### run\_stream\_events ```python def run_stream_events( user_prompt: str | Sequence[_messages.UserContent] | None = None, output_type: None = None, message_history: Sequence[_messages.ModelMessage] | None = None, deferred_tool_results: DeferredToolResults | None = None, conversation_id: str | None = None, model: models.Model | models.KnownModelName | str | None = None, instructions: _instructions.AgentInstructions[AgentDepsT] = None, deps: AgentDepsT = None, model_settings: AgentModelSettings[AgentDepsT] | None = None, usage_limits: _usage.UsageLimits | None = None, usage: _usage.RunUsage | None = None, metadata: AgentMetadata[AgentDepsT] | None = None, retries: int | AgentRetries | None = None, infer_name: bool = True, toolsets: Sequence[AbstractToolset[AgentDepsT]] | None = None, capabilities: Sequence[AgentCapability[AgentDepsT]] | None = None, spec: dict[str, Any] | AgentSpec | None = None, ) -> AgentEventStream[OutputDataT] def run_stream_events( user_prompt: str | Sequence[_messages.UserContent] | None = None, output_type: OutputSpec[RunOutputDataT], message_history: Sequence[_messages.ModelMessage] | None = None, deferred_tool_results: DeferredToolResults | None = None, conversation_id: str | None = None, model: models.Model | models.KnownModelName | str | None = None, instructions: _instructions.AgentInstructions[AgentDepsT] = None, deps: AgentDepsT = None, model_settings: AgentModelSettings[AgentDepsT] | None = None, usage_limits: _usage.UsageLimits | None = None, usage: _usage.RunUsage | None = None, metadata: AgentMetadata[AgentDepsT] | None = None, retries: int | AgentRetries | None = None, infer_name: bool = True, toolsets: Sequence[AbstractToolset[AgentDepsT]] | None = None, capabilities: Sequence[AgentCapability[AgentDepsT]] | None = None, spec: dict[str, Any] | AgentSpec | None = None, ) -> AgentEventStream[RunOutputDataT] ``` Run the agent with a user prompt in async mode and stream events from the run. This is a convenience method that wraps [`self.run`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.AbstractAgent.run) and uses the `event_stream_handler` kwarg to get a stream of events from the run. Example: ```python from pydantic_ai import Agent, AgentRunResultEvent, AgentStreamEvent agent = Agent('openai:gpt-5.2') async def main(): events: list[AgentStreamEvent | AgentRunResultEvent] = [] async with agent.run_stream_events('What is the capital of France?') as stream: async for event in stream: events.append(event) print(events) ''' [ PartStartEvent(index=0, part=TextPart(content='The capital of ')), FinalResultEvent(tool_name=None, tool_call_id=None), PartDeltaEvent(index=0, delta=TextPartDelta(content_delta='France is Paris. ')), PartEndEvent( index=0, part=TextPart(content='The capital of France is Paris. ') ), AgentRunResultEvent( result=AgentRunResult(output='The capital of France is Paris. ') ), ] ''' ``` Arguments are the same as for [`self.run`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.AbstractAgent.run), except that `event_stream_handler` is now allowed. ###### Returns `AgentEventStream`\[[`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] -- An async iterable of stream events `AgentStreamEvent` and finally a `AgentRunResultEvent` with the final `AgentEventStream`\[[`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] -- run result. ###### Parameters **`user_prompt`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[`_messages.UserContent`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` User input to start/continue the conversation. **`output_type`** : `OutputSpec`\[`RunOutputDataT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Custom output type to use for this run, `output_type` may only be used if the agent has no output validators since output validators would expect an argument that matches the agent's output type. **`message_history`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[`_messages.ModelMessage`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` History of the conversation so far. **`deferred_tool_results`** : [`DeferredToolResults`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.DeferredToolResults) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional results for deferred tool calls in the message history. **`conversation_id`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` ID of the conversation this run belongs to. Pass `'new'` to start a fresh conversation, ignoring any `conversation_id` already on `message_history`. If omitted, falls back to the most recent `conversation_id` on `message_history` or a freshly generated UUID7. **`model`** : [`models.Model`](/docs/ai/api/models/base/#pydantic_ai.models.Model) | [`models.KnownModelName`](/docs/ai/api/models/base/#pydantic_ai.models.KnownModelName) | [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional model to use for this run, required if `model` was not set when creating the agent. **`instructions`** : `_instructions.AgentInstructions`\[`AgentDepsT`\] _Default:_ `None` Optional additional instructions to use for this run. **`deps`** : `AgentDepsT` _Default:_ `None` Optional dependencies to use for this run. **`model_settings`** : `AgentModelSettings`\[`AgentDepsT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional settings to use for this model's request. **`usage_limits`** : `_usage.UsageLimits` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional limits on model request count or token usage. **`usage`** : `_usage.RunUsage` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional usage to start with, useful for resuming a conversation or agents used in tools. **`metadata`** : `AgentMetadata`\[`AgentDepsT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional metadata to attach to this run. Accepts a dictionary or a callable taking [`RunContext`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.RunContext); merged with the agent's configured metadata. **`retries`** : [`int`](https://docs.python.org/3/library/functions.html#int) | [`AgentRetries`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.AgentRetries) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Override the agent-level retry budgets for this run. Pass an `int` to override the output-validation budget, or an [`AgentRetries`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.AgentRetries) dict for finer control. Tool retries cannot be overridden per run. See [`Agent.__init__`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.Agent.__init__) for semantics of the two enforcement paths. **`infer_name`** : [`bool`](https://docs.python.org/3/library/functions.html#bool) _Default:_ `True` Whether to try to infer the agent name from the call frame if it's not set. **`toolsets`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`AbstractToolset`](/docs/ai/api/pydantic-ai/toolsets/#pydantic_ai.toolsets.AbstractToolset)\[`AgentDepsT`\]\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional additional toolsets for this run. **`capabilities`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`AgentCapability`](/docs/ai/api/pydantic-ai/capabilities/#pydantic_ai.capabilities.AgentCapability)\[`AgentDepsT`\]\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional additional [capabilities](https://ai.pydantic.dev/capabilities/) for this run, merged with the agent's configured capabilities. **`spec`** : [`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] | `AgentSpec` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional agent spec to apply for this run. ##### iter `@async` ```python def iter( user_prompt: str | Sequence[_messages.UserContent] | None = None, output_type: None = None, message_history: Sequence[_messages.ModelMessage] | None = None, deferred_tool_results: DeferredToolResults | None = None, conversation_id: str | None = None, model: models.Model | models.KnownModelName | str | None = None, instructions: _instructions.AgentInstructions[AgentDepsT] = None, deps: AgentDepsT = None, model_settings: AgentModelSettings[AgentDepsT] | None = None, usage_limits: _usage.UsageLimits | None = None, usage: _usage.RunUsage | None = None, metadata: AgentMetadata[AgentDepsT] | None = None, retries: int | AgentRetries | None = None, infer_name: bool = True, toolsets: Sequence[AbstractToolset[AgentDepsT]] | None = None, capabilities: Sequence[AgentCapability[AgentDepsT]] | None = None, spec: dict[str, Any] | AgentSpec | None = None, _deprecated_kwargs: Any = {}, ) -> AbstractAsyncContextManager[AgentRun[AgentDepsT, OutputDataT]] def iter( user_prompt: str | Sequence[_messages.UserContent] | None = None, output_type: OutputSpec[RunOutputDataT], message_history: Sequence[_messages.ModelMessage] | None = None, deferred_tool_results: DeferredToolResults | None = None, conversation_id: str | None = None, model: models.Model | models.KnownModelName | str | None = None, instructions: _instructions.AgentInstructions[AgentDepsT] = None, deps: AgentDepsT = None, model_settings: AgentModelSettings[AgentDepsT] | None = None, usage_limits: _usage.UsageLimits | None = None, usage: _usage.RunUsage | None = None, metadata: AgentMetadata[AgentDepsT] | None = None, retries: int | AgentRetries | None = None, infer_name: bool = True, toolsets: Sequence[AbstractToolset[AgentDepsT]] | None = None, capabilities: Sequence[AgentCapability[AgentDepsT]] | None = None, spec: dict[str, Any] | AgentSpec | None = None, _deprecated_kwargs: Any = {}, ) -> AbstractAsyncContextManager[AgentRun[AgentDepsT, RunOutputDataT]] ``` A contextmanager which can be used to iterate over the agent graph's nodes as they are executed. This method builds an internal agent graph (using system prompts, tools and output schemas) and then returns an `AgentRun` object. The `AgentRun` can be used to async-iterate over the nodes of the graph as they are executed. This is the API to use if you want to consume the outputs coming from each LLM model response, or the stream of events coming from the execution of tools. The `AgentRun` also provides methods to access the full message history, new messages, and usage statistics, and the final result of the run once it has completed. For more details, see the documentation of `AgentRun`. Example: ```python from pydantic_ai import Agent agent = Agent('openai:gpt-5.2') async def main(): nodes = [] async with agent.iter('What is the capital of France?') as agent_run: async for node in agent_run: nodes.append(node) print(nodes) ''' [ UserPromptNode( user_prompt='What is the capital of France?', instructions_functions=[], system_prompts=(), system_prompt_functions=[], system_prompt_dynamic_functions={}, ), ModelRequestNode( request=ModelRequest( parts=[ UserPromptPart( content='What is the capital of France?', timestamp=datetime.datetime(...), ) ], timestamp=datetime.datetime(...), run_id='...', conversation_id='...', ) ), CallToolsNode( model_response=ModelResponse( parts=[TextPart(content='The capital of France is Paris.')], usage=RequestUsage(input_tokens=56, output_tokens=7), model_name='gpt-5.2', timestamp=datetime.datetime(...), run_id='...', conversation_id='...', ) ), End(data=FinalResult(output='The capital of France is Paris.')), ] ''' print(agent_run.result.output) #> The capital of France is Paris. ``` ###### Returns [`AsyncIterator`](https://docs.python.org/3/library/typing.html#typing.AsyncIterator)\[[`AgentRun`](/docs/ai/api/pydantic-ai/run/#pydantic_ai.run.AgentRun)\[`AgentDepsT`, [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\]\] -- The result of the run. ###### Parameters **`user_prompt`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[`_messages.UserContent`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` User input to start/continue the conversation. **`output_type`** : `OutputSpec`\[`RunOutputDataT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Custom output type to use for this run, `output_type` may only be used if the agent has no output validators since output validators would expect an argument that matches the agent's output type. **`message_history`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[`_messages.ModelMessage`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` History of the conversation so far. **`deferred_tool_results`** : [`DeferredToolResults`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.DeferredToolResults) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional results for deferred tool calls in the message history. **`conversation_id`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` ID of the conversation this run belongs to. Pass `'new'` to start a fresh conversation, ignoring any `conversation_id` already on `message_history`. If omitted, falls back to the most recent `conversation_id` on `message_history` or a freshly generated UUID7. **`model`** : [`models.Model`](/docs/ai/api/models/base/#pydantic_ai.models.Model) | [`models.KnownModelName`](/docs/ai/api/models/base/#pydantic_ai.models.KnownModelName) | [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional model to use for this run, required if `model` was not set when creating the agent. **`instructions`** : `_instructions.AgentInstructions`\[`AgentDepsT`\] _Default:_ `None` Optional additional instructions to use for this run. **`deps`** : `AgentDepsT` _Default:_ `None` Optional dependencies to use for this run. **`model_settings`** : `AgentModelSettings`\[`AgentDepsT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional settings to use for this model's request. **`usage_limits`** : `_usage.UsageLimits` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional limits on model request count or token usage. **`usage`** : `_usage.RunUsage` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional usage to start with, useful for resuming a conversation or agents used in tools. **`metadata`** : `AgentMetadata`\[`AgentDepsT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional metadata to attach to this run. Accepts a dictionary or a callable taking [`RunContext`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.RunContext); merged with the agent's configured metadata. **`retries`** : [`int`](https://docs.python.org/3/library/functions.html#int) | [`AgentRetries`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.AgentRetries) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Override the agent-level retry budgets for this run. Pass an `int` to override the output-validation budget, or an [`AgentRetries`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.AgentRetries) dict for finer control. Tool retries cannot be overridden per run. See [`Agent.__init__`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.Agent.__init__) for semantics of the two enforcement paths. **`infer_name`** : [`bool`](https://docs.python.org/3/library/functions.html#bool) _Default:_ `True` Whether to try to infer the agent name from the call frame if it's not set. **`toolsets`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`AbstractToolset`](/docs/ai/api/pydantic-ai/toolsets/#pydantic_ai.toolsets.AbstractToolset)\[`AgentDepsT`\]\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional additional toolsets for this run. **`capabilities`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`AgentCapability`](/docs/ai/api/pydantic-ai/capabilities/#pydantic_ai.capabilities.AgentCapability)\[`AgentDepsT`\]\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional additional [capabilities](https://ai.pydantic.dev/capabilities/) for this run, merged with the agent's configured capabilities. **`spec`** : [`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] | `AgentSpec` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional agent spec to apply for this run. ##### override ```python def override( name: str | _utils.Unset = _utils.UNSET, deps: AgentDepsT | _utils.Unset = _utils.UNSET, model: models.Model | models.KnownModelName | str | _utils.Unset = _utils.UNSET, toolsets: Sequence[AbstractToolset[AgentDepsT]] | _utils.Unset = _utils.UNSET, tools: Sequence[Tool[AgentDepsT] | ToolFuncEither[AgentDepsT, ...]] | _utils.Unset = _utils.UNSET, native_tools: Sequence[AgentNativeTool[AgentDepsT]] | _utils.Unset = _utils.UNSET, instructions: _instructions.AgentInstructions[AgentDepsT] | _utils.Unset = _utils.UNSET, model_settings: AgentModelSettings[AgentDepsT] | _utils.Unset = _utils.UNSET, retries: int | AgentRetries | _utils.Unset = _utils.UNSET, spec: dict[str, Any] | AgentSpec | None = None, _deprecated_kwargs: Any = {}, ) -> Iterator[None] ``` Context manager to temporarily override agent configuration. This is particularly useful when testing. You can find an example of this [here](/docs/ai/guides/testing#overriding-model-via-pytest-fixtures). ###### Returns [`Iterator`](https://docs.python.org/3/library/typing.html#typing.Iterator)\[[`None`](https://docs.python.org/3/library/constants.html#None)\] ###### Parameters **`name`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | `_utils.Unset` _Default:_ `_utils.UNSET` The name to use instead of the name passed to the agent constructor and agent run. **`deps`** : `AgentDepsT` | `_utils.Unset` _Default:_ `_utils.UNSET` The dependencies to use instead of the dependencies passed to the agent run. **`model`** : [`models.Model`](/docs/ai/api/models/base/#pydantic_ai.models.Model) | [`models.KnownModelName`](/docs/ai/api/models/base/#pydantic_ai.models.KnownModelName) | [`str`](https://docs.python.org/3/library/stdtypes.html#str) | `_utils.Unset` _Default:_ `_utils.UNSET` The model to use instead of the model passed to the agent run. **`toolsets`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`AbstractToolset`](/docs/ai/api/pydantic-ai/toolsets/#pydantic_ai.toolsets.AbstractToolset)\[`AgentDepsT`\]\] | `_utils.Unset` _Default:_ `_utils.UNSET` The toolsets to use instead of the toolsets passed to the agent constructor and agent run. **`tools`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`Tool`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.Tool)\[`AgentDepsT`\] | `ToolFuncEither`\[`AgentDepsT`, ...\]\] | `_utils.Unset` _Default:_ `_utils.UNSET` The tools to use instead of the tools registered with the agent. **`native_tools`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`AgentNativeTool`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.AgentNativeTool)\[`AgentDepsT`\]\] | `_utils.Unset` _Default:_ `_utils.UNSET` The native tools to use instead of the agent's configured native tools. **`instructions`** : `_instructions.AgentInstructions`\[`AgentDepsT`\] | `_utils.Unset` _Default:_ `_utils.UNSET` The instructions to use instead of the instructions registered with the agent. **`model_settings`** : `AgentModelSettings`\[`AgentDepsT`\] | `_utils.Unset` _Default:_ `_utils.UNSET` The model settings to use instead of the model settings passed to the agent constructor. When set, any per-run `model_settings` argument is ignored. **`retries`** : [`int`](https://docs.python.org/3/library/functions.html#int) | [`AgentRetries`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.AgentRetries) | `_utils.Unset` _Default:_ `_utils.UNSET` The retry budgets to use instead of the agent-level configuration. Pass an `int` to override the output-validation budget, or an [`AgentRetries`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.AgentRetries) dict for finer control. When set, any per-run `retries` argument is ignored. **`spec`** : [`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] | `AgentSpec` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional agent spec to apply as overrides. ### DBOSParallelExecutionMode The mode for executing tool calls in DBOS durable workflows. This is a subset of the ParallelExecutionMode because 'parallel' cannot guarantee deterministic ordering. **Default:** `Literal['sequential', 'parallel_ordered_events']` ### TaskConfig **Bases:** [`TypedDict`](https://docs.python.org/3/library/typing.html#typing.TypedDict) Configuration for a task in Prefect. These options are passed to the `@task` decorator. #### Attributes ##### retries Maximum number of retries for the task. **Type:** [`int`](https://docs.python.org/3/library/functions.html#int) ##### retry\_delay\_seconds Delay between retries in seconds. Can be a single value or a list for custom backoff. **Type:** [`float`](https://docs.python.org/3/library/functions.html#float) | [`list`](https://docs.python.org/3/glossary.html#term-list)\[[`float`](https://docs.python.org/3/library/functions.html#float)\] ##### timeout\_seconds Maximum time in seconds for the task to complete. **Type:** [`float`](https://docs.python.org/3/library/functions.html#float) ##### cache\_policy Prefect cache policy for the task. **Type:** `CachePolicy` ##### persist\_result Whether to persist the task result. **Type:** [`bool`](https://docs.python.org/3/library/functions.html#bool) ##### result\_storage Prefect result storage for the task. Should be a storage block or a block slug like `s3-bucket/my-storage`. **Type:** `ResultStorage` ##### log\_prints Whether to log print statements from the task. **Type:** [`bool`](https://docs.python.org/3/library/functions.html#bool) ### PrefectFunctionToolset **Bases:** `PrefectWrapperToolset[AgentDepsT]` A wrapper for FunctionToolset that integrates with Prefect, turning tool calls into Prefect tasks. #### Methods ##### call\_tool `@async` ```python def call_tool( name: str, tool_args: dict[str, Any], ctx: RunContext[AgentDepsT], tool: ToolsetTool[AgentDepsT], ) -> Any ``` Call a tool, wrapped as a Prefect task with a descriptive name. ###### Returns [`Any`](https://docs.python.org/3/library/typing.html#typing.Any) ### PrefectMCPServer **Bases:** `PrefectWrapperToolset[AgentDepsT]`, `ABC` A wrapper for MCPServer that integrates with Prefect, turning call\_tool and get\_tools into Prefect tasks. #### Methods ##### call\_tool `@async` ```python def call_tool( name: str, tool_args: dict[str, Any], ctx: RunContext[AgentDepsT], tool: ToolsetTool[AgentDepsT], ) -> ToolResult ``` Call an MCP tool, wrapped as a Prefect task with a descriptive name. ###### Returns `ToolResult` ### PrefectModel **Bases:** `WrapperModel` A wrapper for Model that integrates with Prefect, turning request and request\_stream into Prefect tasks. #### Methods ##### request `@async` ```python def request( messages: list[ModelMessage], model_settings: ModelSettings | None, model_request_parameters: ModelRequestParameters, ) -> ModelResponse ``` Make a model request, wrapped as a Prefect task when in a flow. ###### Returns [`ModelResponse`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.ModelResponse) ##### request\_stream `@async` ```python def request_stream( messages: list[ModelMessage], model_settings: ModelSettings | None, model_request_parameters: ModelRequestParameters, run_context: RunContext[Any] | None = None, ) -> AsyncIterator[StreamedResponse] ``` Make a streaming model request. When inside a Prefect flow, the stream is consumed within a task and a non-streaming response is returned. When not in a flow, behaves normally. ###### Returns [`AsyncIterator`](https://docs.python.org/3/library/typing.html#typing.AsyncIterator)\[`StreamedResponse`\] ### PrefectAgent **Bases:** `WrapperAgent[AgentDepsT, OutputDataT]` #### Methods ##### \_\_init\_\_ ```python def __init__( wrapped: AbstractAgent[AgentDepsT, OutputDataT], name: str | None = None, event_stream_handler: EventStreamHandler[AgentDepsT] | None = None, mcp_task_config: TaskConfig | None = None, model_task_config: TaskConfig | None = None, tool_task_config: TaskConfig | None = None, tool_task_config_by_name: dict[str, TaskConfig | None] | None = None, event_stream_handler_task_config: TaskConfig | None = None, prefectify_toolset_func: Callable[[AbstractToolset[AgentDepsT], TaskConfig, TaskConfig, dict[str, TaskConfig | None]], AbstractToolset[AgentDepsT]] = prefectify_toolset, ) ``` Wrap an agent to enable it with Prefect durable flows, by automatically offloading model requests, tool calls, and MCP server communication to Prefect tasks. After wrapping, the original agent can still be used as normal outside of the Prefect flow. ###### Parameters **`wrapped`** : `AbstractAgent`\[`AgentDepsT`, `OutputDataT`\] The agent to wrap. **`name`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional unique agent name to use as the Prefect flow name prefix. If not provided, the agent's `name` will be used. **`event_stream_handler`** : `EventStreamHandler`\[`AgentDepsT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional event stream handler to use instead of the one set on the wrapped agent. **`mcp_task_config`** : `TaskConfig` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` The base Prefect task config to use for MCP server tasks. If no config is provided, use the default settings of Prefect. **`model_task_config`** : `TaskConfig` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` The Prefect task config to use for model request tasks. If no config is provided, use the default settings of Prefect. **`tool_task_config`** : `TaskConfig` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` The default Prefect task config to use for tool calls. If no config is provided, use the default settings of Prefect. **`tool_task_config_by_name`** : [`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), `TaskConfig` | [`None`](https://docs.python.org/3/library/constants.html#None)\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Per-tool task configuration. Keys are tool names, values are TaskConfig or None (None disables task wrapping for that tool). **`event_stream_handler_task_config`** : `TaskConfig` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` The Prefect task config to use for the event stream handler task. If no config is provided, use the default settings of Prefect. **`prefectify_toolset_func`** : [`Callable`](https://docs.python.org/3/library/typing.html#typing.Callable)\[\[[`AbstractToolset`](/docs/ai/api/pydantic-ai/toolsets/#pydantic_ai.toolsets.AbstractToolset)\[`AgentDepsT`\], `TaskConfig`, `TaskConfig`, [`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), `TaskConfig` | [`None`](https://docs.python.org/3/library/constants.html#None)\]\], [`AbstractToolset`](/docs/ai/api/pydantic-ai/toolsets/#pydantic_ai.toolsets.AbstractToolset)\[`AgentDepsT`\]\] _Default:_ `prefectify_toolset` Optional function to use to prepare toolsets for Prefect by wrapping them in a `PrefectWrapperToolset` that moves methods that require IO to Prefect tasks. If not provided, only `FunctionToolset` and `MCPServer` will be prepared for Prefect. The function takes the toolset, the task config, the tool-specific task config, and the tool-specific task config by name. ##### run `@async` ```python def run( user_prompt: str | Sequence[_messages.UserContent] | None = None, output_type: None = None, message_history: Sequence[_messages.ModelMessage] | None = None, deferred_tool_results: DeferredToolResults | None = None, conversation_id: str | None = None, model: models.Model | models.KnownModelName | str | None = None, instructions: _instructions.AgentInstructions[AgentDepsT] = None, deps: AgentDepsT = None, model_settings: AgentModelSettings[AgentDepsT] | None = None, usage_limits: _usage.UsageLimits | None = None, usage: _usage.RunUsage | None = None, metadata: AgentMetadata[AgentDepsT] | None = None, retries: int | AgentRetries | None = None, infer_name: bool = True, toolsets: Sequence[AbstractToolset[AgentDepsT]] | None = None, event_stream_handler: EventStreamHandler[AgentDepsT] | None = None, capabilities: Sequence[AgentCapability[AgentDepsT]] | None = None, spec: dict[str, Any] | AgentSpec | None = None, ) -> AgentRunResult[OutputDataT] def run( user_prompt: str | Sequence[_messages.UserContent] | None = None, output_type: OutputSpec[RunOutputDataT], message_history: Sequence[_messages.ModelMessage] | None = None, deferred_tool_results: DeferredToolResults | None = None, conversation_id: str | None = None, model: models.Model | models.KnownModelName | str | None = None, instructions: _instructions.AgentInstructions[AgentDepsT] = None, deps: AgentDepsT = None, model_settings: AgentModelSettings[AgentDepsT] | None = None, usage_limits: _usage.UsageLimits | None = None, usage: _usage.RunUsage | None = None, metadata: AgentMetadata[AgentDepsT] | None = None, retries: int | AgentRetries | None = None, infer_name: bool = True, toolsets: Sequence[AbstractToolset[AgentDepsT]] | None = None, event_stream_handler: EventStreamHandler[AgentDepsT] | None = None, capabilities: Sequence[AgentCapability[AgentDepsT]] | None = None, spec: dict[str, Any] | AgentSpec | None = None, ) -> AgentRunResult[RunOutputDataT] ``` Run the agent with a user prompt in async mode. This method builds an internal agent graph (using system prompts, tools and result schemas) and then runs the graph to completion. The result of the run is returned. Example: ```python from pydantic_ai import Agent agent = Agent('openai:gpt-5.2') async def main(): agent_run = await agent.run('What is the capital of France?') print(agent_run.output) #> The capital of France is Paris. ``` ###### Returns [`AgentRunResult`](/docs/ai/api/pydantic-ai/run/#pydantic_ai.run.AgentRunResult)\[[`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] -- The result of the run. ###### Parameters **`user_prompt`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[`_messages.UserContent`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` User input to start/continue the conversation. **`output_type`** : `OutputSpec`\[`RunOutputDataT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Custom output type to use for this run, `output_type` may only be used if the agent has no output validators since output validators would expect an argument that matches the agent's output type. **`message_history`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[`_messages.ModelMessage`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` History of the conversation so far. **`deferred_tool_results`** : [`DeferredToolResults`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.DeferredToolResults) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional results for deferred tool calls in the message history. **`conversation_id`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` ID of the conversation this run belongs to. Pass `'new'` to start a fresh conversation, ignoring any `conversation_id` already on `message_history`. If omitted, falls back to the most recent `conversation_id` on `message_history` or a freshly generated UUID7. **`model`** : [`models.Model`](/docs/ai/api/models/base/#pydantic_ai.models.Model) | [`models.KnownModelName`](/docs/ai/api/models/base/#pydantic_ai.models.KnownModelName) | [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional model to use for this run, required if `model` was not set when creating the agent. **`instructions`** : `_instructions.AgentInstructions`\[`AgentDepsT`\] _Default:_ `None` Optional additional instructions to use for this run. **`deps`** : `AgentDepsT` _Default:_ `None` Optional dependencies to use for this run. **`model_settings`** : `AgentModelSettings`\[`AgentDepsT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional settings to use for this model's request. **`usage_limits`** : `_usage.UsageLimits` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional limits on model request count or token usage. **`usage`** : `_usage.RunUsage` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional usage to start with, useful for resuming a conversation or agents used in tools. **`metadata`** : `AgentMetadata`\[`AgentDepsT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional metadata to attach to this run. Accepts a dictionary or a callable taking [`RunContext`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.RunContext); merged with the agent's configured metadata. **`retries`** : [`int`](https://docs.python.org/3/library/functions.html#int) | [`AgentRetries`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.AgentRetries) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Override the agent-level retry budgets for this run. Pass an `int` to override the output-validation budget, or an [`AgentRetries`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.AgentRetries) dict for finer control. Tool retries cannot be overridden per run. See [`Agent.__init__`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.Agent.__init__) for semantics of the two enforcement paths. **`infer_name`** : [`bool`](https://docs.python.org/3/library/functions.html#bool) _Default:_ `True` Whether to try to infer the agent name from the call frame if it's not set. **`toolsets`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`AbstractToolset`](/docs/ai/api/pydantic-ai/toolsets/#pydantic_ai.toolsets.AbstractToolset)\[`AgentDepsT`\]\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional additional toolsets for this run. **`event_stream_handler`** : `EventStreamHandler`\[`AgentDepsT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional event stream handler to use for this run. **`capabilities`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`AgentCapability`](/docs/ai/api/pydantic-ai/capabilities/#pydantic_ai.capabilities.AgentCapability)\[`AgentDepsT`\]\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional additional [capabilities](https://ai.pydantic.dev/capabilities/) for this run, merged with the agent's configured capabilities. **`spec`** : [`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] | `AgentSpec` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional agent spec to apply for this run. ##### run\_sync ```python def run_sync( user_prompt: str | Sequence[_messages.UserContent] | None = None, output_type: None = None, message_history: Sequence[_messages.ModelMessage] | None = None, deferred_tool_results: DeferredToolResults | None = None, conversation_id: str | None = None, model: models.Model | models.KnownModelName | str | None = None, instructions: _instructions.AgentInstructions[AgentDepsT] = None, deps: AgentDepsT = None, model_settings: AgentModelSettings[AgentDepsT] | None = None, usage_limits: _usage.UsageLimits | None = None, usage: _usage.RunUsage | None = None, metadata: AgentMetadata[AgentDepsT] | None = None, retries: int | AgentRetries | None = None, infer_name: bool = True, toolsets: Sequence[AbstractToolset[AgentDepsT]] | None = None, event_stream_handler: EventStreamHandler[AgentDepsT] | None = None, capabilities: Sequence[AgentCapability[AgentDepsT]] | None = None, spec: dict[str, Any] | AgentSpec | None = None, ) -> AgentRunResult[OutputDataT] def run_sync( user_prompt: str | Sequence[_messages.UserContent] | None = None, output_type: OutputSpec[RunOutputDataT], message_history: Sequence[_messages.ModelMessage] | None = None, deferred_tool_results: DeferredToolResults | None = None, conversation_id: str | None = None, model: models.Model | models.KnownModelName | str | None = None, instructions: _instructions.AgentInstructions[AgentDepsT] = None, deps: AgentDepsT = None, model_settings: AgentModelSettings[AgentDepsT] | None = None, usage_limits: _usage.UsageLimits | None = None, usage: _usage.RunUsage | None = None, metadata: AgentMetadata[AgentDepsT] | None = None, retries: int | AgentRetries | None = None, infer_name: bool = True, toolsets: Sequence[AbstractToolset[AgentDepsT]] | None = None, event_stream_handler: EventStreamHandler[AgentDepsT] | None = None, capabilities: Sequence[AgentCapability[AgentDepsT]] | None = None, spec: dict[str, Any] | AgentSpec | None = None, ) -> AgentRunResult[RunOutputDataT] ``` Synchronously run the agent with a user prompt. This is a convenience method that wraps [`self.run`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.AbstractAgent.run) with `loop.run_until_complete(...)`. You therefore can't use this method inside async code or if there's an active event loop. Example: ```python from pydantic_ai import Agent agent = Agent('openai:gpt-5.2') result_sync = agent.run_sync('What is the capital of Italy?') print(result_sync.output) #> The capital of Italy is Rome. ``` ###### Returns [`AgentRunResult`](/docs/ai/api/pydantic-ai/run/#pydantic_ai.run.AgentRunResult)\[[`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] -- The result of the run. ###### Parameters **`user_prompt`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[`_messages.UserContent`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` User input to start/continue the conversation. **`output_type`** : `OutputSpec`\[`RunOutputDataT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Custom output type to use for this run, `output_type` may only be used if the agent has no output validators since output validators would expect an argument that matches the agent's output type. **`message_history`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[`_messages.ModelMessage`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` History of the conversation so far. **`deferred_tool_results`** : [`DeferredToolResults`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.DeferredToolResults) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional results for deferred tool calls in the message history. **`conversation_id`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` ID of the conversation this run belongs to. Pass `'new'` to start a fresh conversation, ignoring any `conversation_id` already on `message_history`. If omitted, falls back to the most recent `conversation_id` on `message_history` or a freshly generated UUID7. **`model`** : [`models.Model`](/docs/ai/api/models/base/#pydantic_ai.models.Model) | [`models.KnownModelName`](/docs/ai/api/models/base/#pydantic_ai.models.KnownModelName) | [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional model to use for this run, required if `model` was not set when creating the agent. **`instructions`** : `_instructions.AgentInstructions`\[`AgentDepsT`\] _Default:_ `None` Optional additional instructions to use for this run. **`deps`** : `AgentDepsT` _Default:_ `None` Optional dependencies to use for this run. **`model_settings`** : `AgentModelSettings`\[`AgentDepsT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional settings to use for this model's request. **`usage_limits`** : `_usage.UsageLimits` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional limits on model request count or token usage. **`usage`** : `_usage.RunUsage` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional usage to start with, useful for resuming a conversation or agents used in tools. **`metadata`** : `AgentMetadata`\[`AgentDepsT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional metadata to attach to this run. Accepts a dictionary or a callable taking [`RunContext`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.RunContext); merged with the agent's configured metadata. **`retries`** : [`int`](https://docs.python.org/3/library/functions.html#int) | [`AgentRetries`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.AgentRetries) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Override the agent-level retry budgets for this run. Pass an `int` to override the output-validation budget, or an [`AgentRetries`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.AgentRetries) dict for finer control. Tool retries cannot be overridden per run. See [`Agent.__init__`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.Agent.__init__) for semantics of the two enforcement paths. **`infer_name`** : [`bool`](https://docs.python.org/3/library/functions.html#bool) _Default:_ `True` Whether to try to infer the agent name from the call frame if it's not set. **`toolsets`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`AbstractToolset`](/docs/ai/api/pydantic-ai/toolsets/#pydantic_ai.toolsets.AbstractToolset)\[`AgentDepsT`\]\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional additional toolsets for this run. **`event_stream_handler`** : `EventStreamHandler`\[`AgentDepsT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional event stream handler to use for this run. **`capabilities`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`AgentCapability`](/docs/ai/api/pydantic-ai/capabilities/#pydantic_ai.capabilities.AgentCapability)\[`AgentDepsT`\]\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional additional [capabilities](https://ai.pydantic.dev/capabilities/) for this run, merged with the agent's configured capabilities. **`spec`** : [`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] | `AgentSpec` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional agent spec to apply for this run. ##### run\_stream `@async` ```python def run_stream( user_prompt: str | Sequence[_messages.UserContent] | None = None, output_type: None = None, message_history: Sequence[_messages.ModelMessage] | None = None, deferred_tool_results: DeferredToolResults | None = None, conversation_id: str | None = None, model: models.Model | models.KnownModelName | str | None = None, instructions: _instructions.AgentInstructions[AgentDepsT] = None, deps: AgentDepsT = None, model_settings: AgentModelSettings[AgentDepsT] | None = None, usage_limits: _usage.UsageLimits | None = None, usage: _usage.RunUsage | None = None, metadata: AgentMetadata[AgentDepsT] | None = None, retries: int | AgentRetries | None = None, infer_name: bool = True, toolsets: Sequence[AbstractToolset[AgentDepsT]] | None = None, event_stream_handler: EventStreamHandler[AgentDepsT] | None = None, capabilities: Sequence[AgentCapability[AgentDepsT]] | None = None, spec: dict[str, Any] | AgentSpec | None = None, ) -> AbstractAsyncContextManager[StreamedRunResult[AgentDepsT, OutputDataT]] def run_stream( user_prompt: str | Sequence[_messages.UserContent] | None = None, output_type: OutputSpec[RunOutputDataT], message_history: Sequence[_messages.ModelMessage] | None = None, deferred_tool_results: DeferredToolResults | None = None, conversation_id: str | None = None, model: models.Model | models.KnownModelName | str | None = None, instructions: _instructions.AgentInstructions[AgentDepsT] = None, deps: AgentDepsT = None, model_settings: AgentModelSettings[AgentDepsT] | None = None, usage_limits: _usage.UsageLimits | None = None, usage: _usage.RunUsage | None = None, metadata: AgentMetadata[AgentDepsT] | None = None, retries: int | AgentRetries | None = None, infer_name: bool = True, toolsets: Sequence[AbstractToolset[AgentDepsT]] | None = None, event_stream_handler: EventStreamHandler[AgentDepsT] | None = None, capabilities: Sequence[AgentCapability[AgentDepsT]] | None = None, spec: dict[str, Any] | AgentSpec | None = None, ) -> AbstractAsyncContextManager[StreamedRunResult[AgentDepsT, RunOutputDataT]] ``` Run the agent with a user prompt in async mode, returning a streamed response. Example: ```python from pydantic_ai import Agent agent = Agent('openai:gpt-5.2') async def main(): async with agent.run_stream('What is the capital of the UK?') as response: print(await response.get_output()) #> The capital of the UK is London. ``` ###### Returns [`AsyncIterator`](https://docs.python.org/3/library/typing.html#typing.AsyncIterator)\[`StreamedRunResult`\[`AgentDepsT`, [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\]\] -- The result of the run. ###### Parameters **`user_prompt`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[`_messages.UserContent`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` User input to start/continue the conversation. **`output_type`** : `OutputSpec`\[`RunOutputDataT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Custom output type to use for this run, `output_type` may only be used if the agent has no output validators since output validators would expect an argument that matches the agent's output type. **`message_history`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[`_messages.ModelMessage`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` History of the conversation so far. **`deferred_tool_results`** : [`DeferredToolResults`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.DeferredToolResults) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional results for deferred tool calls in the message history. **`conversation_id`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` ID of the conversation this run belongs to. Pass `'new'` to start a fresh conversation, ignoring any `conversation_id` already on `message_history`. If omitted, falls back to the most recent `conversation_id` on `message_history` or a freshly generated UUID7. **`model`** : [`models.Model`](/docs/ai/api/models/base/#pydantic_ai.models.Model) | [`models.KnownModelName`](/docs/ai/api/models/base/#pydantic_ai.models.KnownModelName) | [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional model to use for this run, required if `model` was not set when creating the agent. **`instructions`** : `_instructions.AgentInstructions`\[`AgentDepsT`\] _Default:_ `None` Optional additional instructions to use for this run. **`deps`** : `AgentDepsT` _Default:_ `None` Optional dependencies to use for this run. **`model_settings`** : `AgentModelSettings`\[`AgentDepsT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional settings to use for this model's request. **`usage_limits`** : `_usage.UsageLimits` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional limits on model request count or token usage. **`usage`** : `_usage.RunUsage` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional usage to start with, useful for resuming a conversation or agents used in tools. **`metadata`** : `AgentMetadata`\[`AgentDepsT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional metadata to attach to this run. Accepts a dictionary or a callable taking [`RunContext`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.RunContext); merged with the agent's configured metadata. **`retries`** : [`int`](https://docs.python.org/3/library/functions.html#int) | [`AgentRetries`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.AgentRetries) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Override the agent-level retry budgets for this run. Pass an `int` to override the output-validation budget, or an [`AgentRetries`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.AgentRetries) dict for finer control. Tool retries cannot be overridden per run. See [`Agent.__init__`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.Agent.__init__) for semantics of the two enforcement paths. **`infer_name`** : [`bool`](https://docs.python.org/3/library/functions.html#bool) _Default:_ `True` Whether to try to infer the agent name from the call frame if it's not set. **`toolsets`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`AbstractToolset`](/docs/ai/api/pydantic-ai/toolsets/#pydantic_ai.toolsets.AbstractToolset)\[`AgentDepsT`\]\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional additional toolsets for this run. **`event_stream_handler`** : `EventStreamHandler`\[`AgentDepsT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional event stream handler to use for this run. It will receive all the events up until the final result is found, which you can then read or stream from inside the context manager. **`capabilities`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`AgentCapability`](/docs/ai/api/pydantic-ai/capabilities/#pydantic_ai.capabilities.AgentCapability)\[`AgentDepsT`\]\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional additional [capabilities](https://ai.pydantic.dev/capabilities/) for this run, merged with the agent's configured capabilities. **`spec`** : [`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] | `AgentSpec` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional agent spec to apply for this run. ##### run\_stream\_events ```python def run_stream_events( user_prompt: str | Sequence[_messages.UserContent] | None = None, output_type: None = None, message_history: Sequence[_messages.ModelMessage] | None = None, deferred_tool_results: DeferredToolResults | None = None, conversation_id: str | None = None, model: models.Model | models.KnownModelName | str | None = None, instructions: _instructions.AgentInstructions[AgentDepsT] = None, deps: AgentDepsT = None, model_settings: AgentModelSettings[AgentDepsT] | None = None, usage_limits: _usage.UsageLimits | None = None, usage: _usage.RunUsage | None = None, metadata: AgentMetadata[AgentDepsT] | None = None, retries: int | AgentRetries | None = None, infer_name: bool = True, toolsets: Sequence[AbstractToolset[AgentDepsT]] | None = None, capabilities: Sequence[AgentCapability[AgentDepsT]] | None = None, spec: dict[str, Any] | AgentSpec | None = None, ) -> AgentEventStream[OutputDataT] def run_stream_events( user_prompt: str | Sequence[_messages.UserContent] | None = None, output_type: OutputSpec[RunOutputDataT], message_history: Sequence[_messages.ModelMessage] | None = None, deferred_tool_results: DeferredToolResults | None = None, conversation_id: str | None = None, model: models.Model | models.KnownModelName | str | None = None, instructions: _instructions.AgentInstructions[AgentDepsT] = None, deps: AgentDepsT = None, model_settings: AgentModelSettings[AgentDepsT] | None = None, usage_limits: _usage.UsageLimits | None = None, usage: _usage.RunUsage | None = None, metadata: AgentMetadata[AgentDepsT] | None = None, retries: int | AgentRetries | None = None, infer_name: bool = True, toolsets: Sequence[AbstractToolset[AgentDepsT]] | None = None, capabilities: Sequence[AgentCapability[AgentDepsT]] | None = None, spec: dict[str, Any] | AgentSpec | None = None, ) -> AgentEventStream[RunOutputDataT] ``` Run the agent with a user prompt in async mode and stream events from the run. This is a convenience method that wraps [`self.run`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.AbstractAgent.run) and uses the `event_stream_handler` kwarg to get a stream of events from the run. Example: ```python from pydantic_ai import Agent, AgentRunResultEvent, AgentStreamEvent agent = Agent('openai:gpt-5.2') async def main(): events: list[AgentStreamEvent | AgentRunResultEvent] = [] async with agent.run_stream_events('What is the capital of France?') as stream: async for event in stream: events.append(event) print(events) ''' [ PartStartEvent(index=0, part=TextPart(content='The capital of ')), FinalResultEvent(tool_name=None, tool_call_id=None), PartDeltaEvent(index=0, delta=TextPartDelta(content_delta='France is Paris. ')), PartEndEvent( index=0, part=TextPart(content='The capital of France is Paris. ') ), AgentRunResultEvent( result=AgentRunResult(output='The capital of France is Paris. ') ), ] ''' ``` Arguments are the same as for [`self.run`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.AbstractAgent.run), except that `event_stream_handler` is now allowed. ###### Returns `AgentEventStream`\[[`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] -- An async iterable of stream events `AgentStreamEvent` and finally a `AgentRunResultEvent` with the final `AgentEventStream`\[[`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] -- run result. ###### Parameters **`user_prompt`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[`_messages.UserContent`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` User input to start/continue the conversation. **`output_type`** : `OutputSpec`\[`RunOutputDataT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Custom output type to use for this run, `output_type` may only be used if the agent has no output validators since output validators would expect an argument that matches the agent's output type. **`message_history`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[`_messages.ModelMessage`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` History of the conversation so far. **`deferred_tool_results`** : [`DeferredToolResults`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.DeferredToolResults) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional results for deferred tool calls in the message history. **`conversation_id`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` ID of the conversation this run belongs to. Pass `'new'` to start a fresh conversation, ignoring any `conversation_id` already on `message_history`. If omitted, falls back to the most recent `conversation_id` on `message_history` or a freshly generated UUID7. **`model`** : [`models.Model`](/docs/ai/api/models/base/#pydantic_ai.models.Model) | [`models.KnownModelName`](/docs/ai/api/models/base/#pydantic_ai.models.KnownModelName) | [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional model to use for this run, required if `model` was not set when creating the agent. **`instructions`** : `_instructions.AgentInstructions`\[`AgentDepsT`\] _Default:_ `None` Optional additional instructions to use for this run. **`deps`** : `AgentDepsT` _Default:_ `None` Optional dependencies to use for this run. **`model_settings`** : `AgentModelSettings`\[`AgentDepsT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional settings to use for this model's request. **`usage_limits`** : `_usage.UsageLimits` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional limits on model request count or token usage. **`usage`** : `_usage.RunUsage` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional usage to start with, useful for resuming a conversation or agents used in tools. **`metadata`** : `AgentMetadata`\[`AgentDepsT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional metadata to attach to this run. Accepts a dictionary or a callable taking [`RunContext`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.RunContext); merged with the agent's configured metadata. **`retries`** : [`int`](https://docs.python.org/3/library/functions.html#int) | [`AgentRetries`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.AgentRetries) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Override the agent-level retry budgets for this run. Pass an `int` to override the output-validation budget, or an [`AgentRetries`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.AgentRetries) dict for finer control. Tool retries cannot be overridden per run. See [`Agent.__init__`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.Agent.__init__) for semantics of the two enforcement paths. **`infer_name`** : [`bool`](https://docs.python.org/3/library/functions.html#bool) _Default:_ `True` Whether to try to infer the agent name from the call frame if it's not set. **`toolsets`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`AbstractToolset`](/docs/ai/api/pydantic-ai/toolsets/#pydantic_ai.toolsets.AbstractToolset)\[`AgentDepsT`\]\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional additional toolsets for this run. **`capabilities`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`AgentCapability`](/docs/ai/api/pydantic-ai/capabilities/#pydantic_ai.capabilities.AgentCapability)\[`AgentDepsT`\]\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional additional [capabilities](https://ai.pydantic.dev/capabilities/) for this run, merged with the agent's configured capabilities. **`spec`** : [`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] | `AgentSpec` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional agent spec to apply for this run. ##### iter `@async` ```python def iter( user_prompt: str | Sequence[_messages.UserContent] | None = None, output_type: None = None, message_history: Sequence[_messages.ModelMessage] | None = None, deferred_tool_results: DeferredToolResults | None = None, conversation_id: str | None = None, model: models.Model | models.KnownModelName | str | None = None, instructions: _instructions.AgentInstructions[AgentDepsT] = None, deps: AgentDepsT = None, model_settings: AgentModelSettings[AgentDepsT] | None = None, usage_limits: _usage.UsageLimits | None = None, usage: _usage.RunUsage | None = None, metadata: AgentMetadata[AgentDepsT] | None = None, retries: int | AgentRetries | None = None, infer_name: bool = True, toolsets: Sequence[AbstractToolset[AgentDepsT]] | None = None, capabilities: Sequence[AgentCapability[AgentDepsT]] | None = None, spec: dict[str, Any] | AgentSpec | None = None, ) -> AbstractAsyncContextManager[AgentRun[AgentDepsT, OutputDataT]] def iter( user_prompt: str | Sequence[_messages.UserContent] | None = None, output_type: OutputSpec[RunOutputDataT], message_history: Sequence[_messages.ModelMessage] | None = None, deferred_tool_results: DeferredToolResults | None = None, conversation_id: str | None = None, model: models.Model | models.KnownModelName | str | None = None, instructions: _instructions.AgentInstructions[AgentDepsT] = None, deps: AgentDepsT = None, model_settings: AgentModelSettings[AgentDepsT] | None = None, usage_limits: _usage.UsageLimits | None = None, usage: _usage.RunUsage | None = None, metadata: AgentMetadata[AgentDepsT] | None = None, retries: int | AgentRetries | None = None, infer_name: bool = True, toolsets: Sequence[AbstractToolset[AgentDepsT]] | None = None, capabilities: Sequence[AgentCapability[AgentDepsT]] | None = None, spec: dict[str, Any] | AgentSpec | None = None, ) -> AbstractAsyncContextManager[AgentRun[AgentDepsT, RunOutputDataT]] ``` A contextmanager which can be used to iterate over the agent graph's nodes as they are executed. This method builds an internal agent graph (using system prompts, tools and output schemas) and then returns an `AgentRun` object. The `AgentRun` can be used to async-iterate over the nodes of the graph as they are executed. This is the API to use if you want to consume the outputs coming from each LLM model response, or the stream of events coming from the execution of tools. The `AgentRun` also provides methods to access the full message history, new messages, and usage statistics, and the final result of the run once it has completed. For more details, see the documentation of `AgentRun`. Example: ```python from pydantic_ai import Agent agent = Agent('openai:gpt-5.2') async def main(): nodes = [] async with agent.iter('What is the capital of France?') as agent_run: async for node in agent_run: nodes.append(node) print(nodes) ''' [ UserPromptNode( user_prompt='What is the capital of France?', instructions_functions=[], system_prompts=(), system_prompt_functions=[], system_prompt_dynamic_functions={}, ), ModelRequestNode( request=ModelRequest( parts=[ UserPromptPart( content='What is the capital of France?', timestamp=datetime.datetime(...), ) ], timestamp=datetime.datetime(...), run_id='...', conversation_id='...', ) ), CallToolsNode( model_response=ModelResponse( parts=[TextPart(content='The capital of France is Paris.')], usage=RequestUsage(input_tokens=56, output_tokens=7), model_name='gpt-5.2', timestamp=datetime.datetime(...), run_id='...', conversation_id='...', ) ), End(data=FinalResult(output='The capital of France is Paris.')), ] ''' print(agent_run.result.output) #> The capital of France is Paris. ``` ###### Returns [`AsyncIterator`](https://docs.python.org/3/library/typing.html#typing.AsyncIterator)\[[`AgentRun`](/docs/ai/api/pydantic-ai/run/#pydantic_ai.run.AgentRun)\[`AgentDepsT`, [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\]\] -- The result of the run. ###### Parameters **`user_prompt`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[`_messages.UserContent`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` User input to start/continue the conversation. **`output_type`** : `OutputSpec`\[`RunOutputDataT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Custom output type to use for this run, `output_type` may only be used if the agent has no output validators since output validators would expect an argument that matches the agent's output type. **`message_history`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[`_messages.ModelMessage`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` History of the conversation so far. **`deferred_tool_results`** : [`DeferredToolResults`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.DeferredToolResults) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional results for deferred tool calls in the message history. **`conversation_id`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` ID of the conversation this run belongs to. Pass `'new'` to start a fresh conversation, ignoring any `conversation_id` already on `message_history`. If omitted, falls back to the most recent `conversation_id` on `message_history` or a freshly generated UUID7. **`model`** : [`models.Model`](/docs/ai/api/models/base/#pydantic_ai.models.Model) | [`models.KnownModelName`](/docs/ai/api/models/base/#pydantic_ai.models.KnownModelName) | [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional model to use for this run, required if `model` was not set when creating the agent. **`deps`** : `AgentDepsT` _Default:_ `None` Optional dependencies to use for this run. **`instructions`** : `_instructions.AgentInstructions`\[`AgentDepsT`\] _Default:_ `None` Optional additional instructions to use for this run. **`model_settings`** : `AgentModelSettings`\[`AgentDepsT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional settings to use for this model's request. **`usage_limits`** : `_usage.UsageLimits` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional limits on model request count or token usage. **`usage`** : `_usage.RunUsage` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional usage to start with, useful for resuming a conversation or agents used in tools. **`metadata`** : `AgentMetadata`\[`AgentDepsT`\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional metadata to attach to this run. Accepts a dictionary or a callable taking [`RunContext`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.RunContext); merged with the agent's configured metadata. **`retries`** : [`int`](https://docs.python.org/3/library/functions.html#int) | [`AgentRetries`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.AgentRetries) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Override the agent-level retry budgets for this run. Pass an `int` to override the output-validation budget, or an [`AgentRetries`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.AgentRetries) dict for finer control. Tool retries cannot be overridden per run. See [`Agent.__init__`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.Agent.__init__) for semantics of the two enforcement paths. **`infer_name`** : [`bool`](https://docs.python.org/3/library/functions.html#bool) _Default:_ `True` Whether to try to infer the agent name from the call frame if it's not set. **`toolsets`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`AbstractToolset`](/docs/ai/api/pydantic-ai/toolsets/#pydantic_ai.toolsets.AbstractToolset)\[`AgentDepsT`\]\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional additional toolsets for this run. **`capabilities`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`AgentCapability`](/docs/ai/api/pydantic-ai/capabilities/#pydantic_ai.capabilities.AgentCapability)\[`AgentDepsT`\]\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional additional [capabilities](https://ai.pydantic.dev/capabilities/) for this run, merged with the agent's configured capabilities. **`spec`** : [`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] | `AgentSpec` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional agent spec to apply for this run. ##### override ```python def override( name: str | _utils.Unset = _utils.UNSET, deps: AgentDepsT | _utils.Unset = _utils.UNSET, model: models.Model | models.KnownModelName | str | _utils.Unset = _utils.UNSET, toolsets: Sequence[AbstractToolset[AgentDepsT]] | _utils.Unset = _utils.UNSET, tools: Sequence[Tool[AgentDepsT] | ToolFuncEither[AgentDepsT, ...]] | _utils.Unset = _utils.UNSET, native_tools: Sequence[AgentNativeTool[AgentDepsT]] | _utils.Unset = _utils.UNSET, instructions: _instructions.AgentInstructions[AgentDepsT] | _utils.Unset = _utils.UNSET, model_settings: AgentModelSettings[AgentDepsT] | _utils.Unset = _utils.UNSET, retries: int | AgentRetries | _utils.Unset = _utils.UNSET, spec: dict[str, Any] | AgentSpec | None = None, _deprecated_kwargs: Any = {}, ) -> Iterator[None] ``` Context manager to temporarily override agent configuration. This is particularly useful when testing. You can find an example of this [here](/docs/ai/guides/testing#overriding-model-via-pytest-fixtures). ###### Returns [`Iterator`](https://docs.python.org/3/library/typing.html#typing.Iterator)\[[`None`](https://docs.python.org/3/library/constants.html#None)\] ###### Parameters **`name`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | `_utils.Unset` _Default:_ `_utils.UNSET` The name to use instead of the name passed to the agent constructor and agent run. **`deps`** : `AgentDepsT` | `_utils.Unset` _Default:_ `_utils.UNSET` The dependencies to use instead of the dependencies passed to the agent run. **`model`** : [`models.Model`](/docs/ai/api/models/base/#pydantic_ai.models.Model) | [`models.KnownModelName`](/docs/ai/api/models/base/#pydantic_ai.models.KnownModelName) | [`str`](https://docs.python.org/3/library/stdtypes.html#str) | `_utils.Unset` _Default:_ `_utils.UNSET` The model to use instead of the model passed to the agent run. **`toolsets`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`AbstractToolset`](/docs/ai/api/pydantic-ai/toolsets/#pydantic_ai.toolsets.AbstractToolset)\[`AgentDepsT`\]\] | `_utils.Unset` _Default:_ `_utils.UNSET` The toolsets to use instead of the toolsets passed to the agent constructor and agent run. **`tools`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`Tool`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.Tool)\[`AgentDepsT`\] | `ToolFuncEither`\[`AgentDepsT`, ...\]\] | `_utils.Unset` _Default:_ `_utils.UNSET` The tools to use instead of the tools registered with the agent. **`native_tools`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`AgentNativeTool`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.AgentNativeTool)\[`AgentDepsT`\]\] | `_utils.Unset` _Default:_ `_utils.UNSET` The native tools to use instead of the agent's configured native tools. **`instructions`** : `_instructions.AgentInstructions`\[`AgentDepsT`\] | `_utils.Unset` _Default:_ `_utils.UNSET` The instructions to use instead of the instructions registered with the agent. **`model_settings`** : `AgentModelSettings`\[`AgentDepsT`\] | `_utils.Unset` _Default:_ `_utils.UNSET` The model settings to use instead of the model settings passed to the agent constructor. When set, any per-run `model_settings` argument is ignored. **`retries`** : [`int`](https://docs.python.org/3/library/functions.html#int) | [`AgentRetries`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.AgentRetries) | `_utils.Unset` _Default:_ `_utils.UNSET` The retry budgets to use instead of the agent-level configuration. Pass an `int` to override the output-validation budget, or an [`AgentRetries`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.AgentRetries) dict for finer control. When set, any per-run `retries` argument is ignored. **`spec`** : [`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] | `AgentSpec` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional agent spec to apply as overrides. --- # [pydantic_ai.embeddings](https://pydantic.dev/docs/ai/api/pydantic-ai/embeddings/) # pydantic\_ai.embeddings ### EmbeddingSettings **Bases:** [`TypedDict`](https://docs.python.org/3/library/typing.html#typing.TypedDict) Common settings for configuring embedding models. These settings apply across multiple embedding model providers. Not all settings are supported by all models - check the specific model's documentation for details. Provider-specific settings classes (e.g., [`OpenAIEmbeddingSettings`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.openai.OpenAIEmbeddingSettings), [`CohereEmbeddingSettings`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.cohere.CohereEmbeddingSettings)) extend this with additional provider-prefixed options. #### Attributes ##### dimensions The number of dimensions for the output embeddings. Supported by: - OpenAI - Cohere - Google - Sentence Transformers - Bedrock - VoyageAI **Type:** [`int`](https://docs.python.org/3/library/functions.html#int) ##### truncate Whether to truncate inputs that exceed the model's context length. Defaults to `False`. If `True`, inputs that are too long will be truncated. If `False`, an error will be raised for inputs that exceed the context length. For more control over truncation, you can use [`max_input_tokens()`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.Embedder.max_input_tokens) and [`count_tokens()`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.Embedder.count_tokens) to implement your own truncation logic. Provider-specific truncation settings (e.g., `cohere_truncate`, `bedrock_cohere_truncate`) take precedence if specified. Supported by: - Cohere - Bedrock (Cohere and Nova models) - VoyageAI **Type:** [`bool`](https://docs.python.org/3/library/functions.html#bool) ##### extra\_headers Extra headers to send to the model. Supported by: - OpenAI - Cohere **Type:** [`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), [`str`](https://docs.python.org/3/library/stdtypes.html#str)\] ##### extra\_body Extra body to send to the model. Supported by: - OpenAI - Cohere **Type:** [`object`](https://docs.python.org/3/glossary.html#term-object) ### EmbeddingModel **Bases:** `ABC` Abstract base class for embedding models. Implement this class to create a custom embedding model. For most use cases, use one of the built-in implementations: - [`OpenAIEmbeddingModel`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.openai.OpenAIEmbeddingModel) - [`CohereEmbeddingModel`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.cohere.CohereEmbeddingModel) - [`GoogleEmbeddingModel`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.google.GoogleEmbeddingModel) - [`BedrockEmbeddingModel`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.bedrock.BedrockEmbeddingModel) - [`SentenceTransformerEmbeddingModel`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.sentence_transformers.SentenceTransformerEmbeddingModel) #### Attributes ##### settings Get the default settings for this model. **Type:** [`EmbeddingSettings`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.EmbeddingSettings) | [`None`](https://docs.python.org/3/library/constants.html#None) ##### base\_url The base URL for the provider API, if available. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) ##### model\_name The name of the embedding model. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### system The embedding model provider/system identifier (e.g., 'openai', 'cohere'). **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) #### Methods ##### \_\_init\_\_ ```python def __init__(settings: EmbeddingSettings | None = None) -> None ``` Initialize the model with optional settings. ###### Returns [`None`](https://docs.python.org/3/library/constants.html#None) ###### Parameters **`settings`** : [`EmbeddingSettings`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.EmbeddingSettings) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Model-specific settings that will be used as defaults for this model. ##### embed `@abstractmethod` `@async` ```python def embed( inputs: str | Sequence[str], input_type: EmbedInputType, settings: EmbeddingSettings | None = None, ) -> EmbeddingResult ``` Generate embeddings for the given inputs. ###### Returns [`EmbeddingResult`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.EmbeddingResult) -- An [`EmbeddingResult`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.EmbeddingResult) containing [`EmbeddingResult`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.EmbeddingResult) -- the embeddings and metadata. ###### Parameters **`inputs`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str)\] A single string or sequence of strings to embed. **`input_type`** : `EmbedInputType` Whether the inputs are queries or documents. **`settings`** : [`EmbeddingSettings`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.EmbeddingSettings) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional settings to override the model's defaults. ##### prepare\_embed ```python def prepare_embed( inputs: str | Sequence[str], settings: EmbeddingSettings | None = None, ) -> tuple[list[str], EmbeddingSettings] ``` Prepare the inputs and settings for embedding. This method normalizes inputs to a list and merges settings. Subclasses should call this at the start of their `embed()` implementation. ###### Returns [`tuple`](https://docs.python.org/3/library/stdtypes.html#tuple)\[[`list`](https://docs.python.org/3/glossary.html#term-list)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str)\], [`EmbeddingSettings`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.EmbeddingSettings)\] -- A tuple of (normalized inputs list, merged settings). ###### Parameters **`inputs`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str)\] A single string or sequence of strings. **`settings`** : [`EmbeddingSettings`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.EmbeddingSettings) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional settings to merge with defaults. ##### max\_input\_tokens `@async` ```python def max_input_tokens() -> int | None ``` Get the maximum number of tokens that can be input to the model. ###### Returns [`int`](https://docs.python.org/3/library/functions.html#int) | [`None`](https://docs.python.org/3/library/constants.html#None) -- The maximum token count, or `None` if unknown. ##### count\_tokens `@async` ```python def count_tokens(text: str) -> int ``` Count the number of tokens in the given text. ###### Returns [`int`](https://docs.python.org/3/library/functions.html#int) -- The number of tokens. ###### Parameters **`text`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) The text to tokenize and count. ###### Raises - `NotImplementedError` -- If the model doesn't support token counting. - `UserError` -- If the model or tokenizer is not supported. ### WrapperEmbeddingModel **Bases:** [`EmbeddingModel`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.EmbeddingModel) Base class for embedding models that wrap another model. Use this as a base class to create custom embedding model wrappers that modify behavior (e.g., caching, logging, rate limiting) while delegating to an underlying model. By default, all methods are passed through to the wrapped model. Override specific methods to customize behavior. #### Attributes ##### wrapped The underlying embedding model being wrapped. **Type:** [`EmbeddingModel`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.EmbeddingModel) **Default:** `infer_embedding_model(wrapped) if isinstance(wrapped, str) else wrapped` ##### settings Get the settings from the wrapped embedding model. **Type:** [`EmbeddingSettings`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.EmbeddingSettings) | [`None`](https://docs.python.org/3/library/constants.html#None) #### Methods ##### \_\_init\_\_ ```python def __init__(wrapped: EmbeddingModel | str) ``` Initialize the wrapper with an embedding model. ###### Parameters **`wrapped`** : [`EmbeddingModel`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.EmbeddingModel) | [`str`](https://docs.python.org/3/library/stdtypes.html#str) The model to wrap. Can be an [`EmbeddingModel`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.EmbeddingModel) instance or a model name string (e.g., `'openai:text-embedding-3-small'`). ### EmbeddingResult The result of an embedding operation. This class contains the generated embeddings along with metadata about the operation, including the original inputs, model information, usage statistics, and timing. Example: ```python from pydantic_ai import Embedder embedder = Embedder('openai:text-embedding-3-small') async def main(): result = await embedder.embed_query('What is AI?') # Access embeddings by index print(len(result.embeddings[0])) #> 1536 # Access embeddings by original input text print(result['What is AI?'] == result.embeddings[0]) #> True # Check usage print(f'Tokens used: {result.usage.input_tokens}') #> Tokens used: 3 ``` #### Attributes ##### embeddings The computed embedding vectors, one per input text. Each embedding is a sequence of floats representing the text in vector space. **Type:** [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`float`](https://docs.python.org/3/library/functions.html#float)\]\] ##### inputs The original input texts that were embedded. **Type:** [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str)\] ##### input\_type Whether the inputs were embedded as queries or documents. **Type:** `EmbedInputType` ##### model\_name The name of the model that generated these embeddings. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### provider\_name The name of the provider (e.g., 'openai', 'cohere'). **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### timestamp When the embedding request was made. **Type:** [`datetime`](https://docs.python.org/3/library/datetime.html#module-datetime) **Default:** `field(default_factory=_now_utc)` ##### usage Token usage statistics for this request. **Type:** [`RequestUsage`](/docs/ai/api/pydantic-ai/usage/#pydantic_ai.usage.RequestUsage) **Default:** `field(default_factory=RequestUsage)` ##### provider\_details Provider-specific details from the response. **Type:** [`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `None` ##### provider\_response\_id Unique identifier for this response from the provider, if available. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `None` #### Methods ##### \_\_getitem\_\_ ```python def __getitem__(item: int | str) -> Sequence[float] ``` Get the embedding for an input by index or by the original input text. ###### Returns [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`float`](https://docs.python.org/3/library/functions.html#float)\] -- The embedding vector for the specified input. ###### Parameters **`item`** : [`int`](https://docs.python.org/3/library/functions.html#int) | [`str`](https://docs.python.org/3/library/stdtypes.html#str) Either an integer index or the original input string. ###### Raises - `IndexError` -- If the index is out of range. - `ValueError` -- If the string is not found in the inputs. ##### cost ```python def cost() -> genai_types.PriceCalculation ``` Calculate the cost of the embedding request. Uses [`genai-prices`](https://github.com/pydantic/genai-prices) for pricing data. ###### Returns `genai_types.PriceCalculation` -- A price calculation object with `total_price`, `input_price`, and other cost details. ###### Raises - `LookupError` -- If pricing data is not available for this model/provider. ### TestEmbeddingModel **Bases:** [`EmbeddingModel`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.EmbeddingModel) A mock embedding model for testing. This model returns deterministic embeddings (all 1.0 values) and tracks the settings used in the last call via the `last_settings` attribute. Example: ```python from pydantic_ai import Embedder from pydantic_ai.embeddings import TestEmbeddingModel test_model = TestEmbeddingModel() embedder = Embedder('openai:text-embedding-3-small') async def main(): with embedder.override(model=test_model): await embedder.embed_query('test') assert test_model.last_settings is not None ``` #### Attributes ##### last\_settings The settings used in the most recent embed call. **Type:** [`EmbeddingSettings`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.EmbeddingSettings) | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `None` ##### model\_name The embedding model name. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### system The embedding model provider. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) #### Methods ##### \_\_init\_\_ ```python def __init__( model_name: str = 'test', provider_name: str = 'test', dimensions: int = 8, settings: EmbeddingSettings | None = None, ) ``` Initialize the test embedding model. ###### Parameters **`model_name`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) _Default:_ `'test'` The model name to report in results. **`provider_name`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) _Default:_ `'test'` The provider name to report in results. **`dimensions`** : [`int`](https://docs.python.org/3/library/functions.html#int) _Default:_ `8` The number of dimensions for the generated embeddings. **`settings`** : [`EmbeddingSettings`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.EmbeddingSettings) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional default settings for the model. ### InstrumentedEmbeddingModel **Bases:** `WrapperEmbeddingModel` Embedding model which wraps another model so that requests are instrumented with OpenTelemetry. See the [Debugging and Monitoring guide](https://ai.pydantic.dev/logfire/) for more info. #### Attributes ##### instrumentation\_settings Instrumentation settings for this model. **Type:** [`InstrumentationSettings`](/docs/ai/api/models/instrumented/#pydantic_ai.models.instrumented.InstrumentationSettings) **Default:** `options or InstrumentationSettings()` ### Embedder High-level interface for generating text embeddings. The `Embedder` class provides a convenient way to generate vector embeddings from text using various embedding model providers. It handles model inference, settings management, and optional OpenTelemetry instrumentation. Example: ```python from pydantic_ai import Embedder embedder = Embedder('openai:text-embedding-3-small') async def main(): result = await embedder.embed_query('What is machine learning?') print(result.embeddings[0][:5]) # First 5 dimensions #> [1.0, 1.0, 1.0, 1.0, 1.0] ``` #### Attributes ##### instrument Options to automatically instrument with OpenTelemetry. Set to `True` to use default instrumentation settings, which will use Logfire if it's configured. Set to an instance of [`InstrumentationSettings`](/docs/ai/api/models/instrumented/#pydantic_ai.models.instrumented.InstrumentationSettings) to customize. If this isn't set, then the last value set by [`Embedder.instrument_all()`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.Embedder.instrument_all) will be used, which defaults to False. See the [Debugging and Monitoring guide](https://ai.pydantic.dev/logfire/) for more info. **Type:** [`InstrumentationSettings`](/docs/ai/api/models/instrumented/#pydantic_ai.models.instrumented.InstrumentationSettings) | [`bool`](https://docs.python.org/3/library/functions.html#bool) | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `instrument` ##### model The embedding model used by this embedder. **Type:** [`EmbeddingModel`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.EmbeddingModel) | `KnownEmbeddingModelName` | [`str`](https://docs.python.org/3/library/stdtypes.html#str) #### Methods ##### \_\_init\_\_ ```python def __init__( model: EmbeddingModel | KnownEmbeddingModelName | str, settings: EmbeddingSettings | None = None, defer_model_check: bool = True, instrument: InstrumentationSettings | bool | None = None, ) -> None ``` Initialize an Embedder. ###### Returns [`None`](https://docs.python.org/3/library/constants.html#None) ###### Parameters **`model`** : [`EmbeddingModel`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.EmbeddingModel) | `KnownEmbeddingModelName` | [`str`](https://docs.python.org/3/library/stdtypes.html#str) The embedding model to use. Can be specified as: - A model name string in the format `'provider:model-name'` (e.g., `'openai:text-embedding-3-small'`) - An [`EmbeddingModel`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.EmbeddingModel) instance **`settings`** : [`EmbeddingSettings`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.EmbeddingSettings) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional [`EmbeddingSettings`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.EmbeddingSettings) to use as defaults for all embed calls. **`defer_model_check`** : [`bool`](https://docs.python.org/3/library/functions.html#bool) _Default:_ `True` Whether to defer model validation until first use. Set to `False` to validate the model immediately on construction. **`instrument`** : [`InstrumentationSettings`](/docs/ai/api/models/instrumented/#pydantic_ai.models.instrumented.InstrumentationSettings) | [`bool`](https://docs.python.org/3/library/functions.html#bool) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` OpenTelemetry instrumentation settings. Set to `True` to enable with defaults, or pass an [`InstrumentationSettings`](/docs/ai/api/models/instrumented/#pydantic_ai.models.instrumented.InstrumentationSettings) instance to customize. If `None`, uses the value from [`Embedder.instrument_all()`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.Embedder.instrument_all). ##### instrument\_all `@staticmethod` ```python def instrument_all(instrument: InstrumentationSettings | bool = True) -> None ``` Set the default instrumentation options for all embedders where `instrument` is not explicitly set. This is useful for enabling instrumentation globally without modifying each embedder individually. ###### Returns [`None`](https://docs.python.org/3/library/constants.html#None) ###### Parameters **`instrument`** : [`InstrumentationSettings`](/docs/ai/api/models/instrumented/#pydantic_ai.models.instrumented.InstrumentationSettings) | [`bool`](https://docs.python.org/3/library/functions.html#bool) _Default:_ `True` Instrumentation settings to use as the default. Set to `True` for default settings, `False` to disable, or pass an [`InstrumentationSettings`](/docs/ai/api/models/instrumented/#pydantic_ai.models.instrumented.InstrumentationSettings) instance to customize. ##### override ```python def override( model: EmbeddingModel | KnownEmbeddingModelName | str | _utils.Unset = _utils.UNSET, ) -> Iterator[None] ``` Context manager to temporarily override the embedding model. Useful for testing or dynamically switching models. Example: ```python from pydantic_ai import Embedder embedder = Embedder('openai:text-embedding-3-small') async def main(): # Temporarily use a different model with embedder.override(model='openai:text-embedding-3-large'): result = await embedder.embed_query('test') print(len(result.embeddings[0])) # 3072 dimensions for large model #> 3072 ``` ###### Returns [`Iterator`](https://docs.python.org/3/library/typing.html#typing.Iterator)\[[`None`](https://docs.python.org/3/library/constants.html#None)\] ###### Parameters **`model`** : [`EmbeddingModel`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.EmbeddingModel) | `KnownEmbeddingModelName` | [`str`](https://docs.python.org/3/library/stdtypes.html#str) | `_utils.Unset` _Default:_ `_utils.UNSET` The embedding model to use within this context. ##### embed\_query `@async` ```python def embed_query( query: str | Sequence[str], settings: EmbeddingSettings | None = None, ) -> EmbeddingResult ``` Embed one or more query texts. Use this method when embedding search queries that will be compared against document embeddings. Some models optimize embeddings differently based on whether the input is a query or document. ###### Returns [`EmbeddingResult`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.EmbeddingResult) -- An [`EmbeddingResult`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.EmbeddingResult) containing the embeddings [`EmbeddingResult`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.EmbeddingResult) -- and metadata about the operation. ###### Parameters **`query`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str)\] A single query string or sequence of query strings to embed. **`settings`** : [`EmbeddingSettings`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.EmbeddingSettings) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional settings to override the embedder's default settings for this call. ##### embed\_documents `@async` ```python def embed_documents( documents: str | Sequence[str], settings: EmbeddingSettings | None = None, ) -> EmbeddingResult ``` Embed one or more document texts. Use this method when embedding documents that will be stored and later searched against. Some models optimize embeddings differently based on whether the input is a query or document. ###### Returns [`EmbeddingResult`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.EmbeddingResult) -- An [`EmbeddingResult`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.EmbeddingResult) containing the embeddings [`EmbeddingResult`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.EmbeddingResult) -- and metadata about the operation. ###### Parameters **`documents`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str)\] A single document string or sequence of document strings to embed. **`settings`** : [`EmbeddingSettings`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.EmbeddingSettings) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional settings to override the embedder's default settings for this call. ##### embed `@async` ```python def embed( inputs: str | Sequence[str], input_type: EmbedInputType, settings: EmbeddingSettings | None = None, ) -> EmbeddingResult ``` Embed text inputs with explicit input type specification. This is the low-level embedding method. For most use cases, prefer [`embed_query()`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.Embedder.embed_query) or [`embed_documents()`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.Embedder.embed_documents). ###### Returns [`EmbeddingResult`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.EmbeddingResult) -- An [`EmbeddingResult`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.EmbeddingResult) containing the embeddings [`EmbeddingResult`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.EmbeddingResult) -- and metadata about the operation. ###### Parameters **`inputs`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str)\] A single string or sequence of strings to embed. **`input_type`** : `EmbedInputType` The type of input, either `'query'` or `'document'`. **`settings`** : [`EmbeddingSettings`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.EmbeddingSettings) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional settings to override the embedder's default settings for this call. ##### max\_input\_tokens `@async` ```python def max_input_tokens() -> int | None ``` Get the maximum number of tokens the model can accept as input. ###### Returns [`int`](https://docs.python.org/3/library/functions.html#int) | [`None`](https://docs.python.org/3/library/constants.html#None) -- The maximum token count, or `None` if the limit is unknown for this model. ##### count\_tokens `@async` ```python def count_tokens(text: str) -> int ``` Count the number of tokens in the given text. ###### Returns [`int`](https://docs.python.org/3/library/functions.html#int) -- The number of tokens in the text. ###### Parameters **`text`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) The text to tokenize and count. ###### Raises - `NotImplementedError` -- If the model doesn't support token counting. - `UserError` -- If the model or tokenizer is not supported. ##### embed\_query\_sync ```python def embed_query_sync( query: str | Sequence[str], settings: EmbeddingSettings | None = None, ) -> EmbeddingResult ``` Synchronous version of [`embed_query()`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.Embedder.embed_query). ###### Returns [`EmbeddingResult`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.EmbeddingResult) ##### embed\_documents\_sync ```python def embed_documents_sync( documents: str | Sequence[str], settings: EmbeddingSettings | None = None, ) -> EmbeddingResult ``` Synchronous version of [`embed_documents()`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.Embedder.embed_documents). ###### Returns [`EmbeddingResult`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.EmbeddingResult) ##### embed\_sync ```python def embed_sync( inputs: str | Sequence[str], input_type: EmbedInputType, settings: EmbeddingSettings | None = None, ) -> EmbeddingResult ``` Synchronous version of [`embed()`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.Embedder.embed). ###### Returns [`EmbeddingResult`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.EmbeddingResult) ##### max\_input\_tokens\_sync ```python def max_input_tokens_sync() -> int | None ``` Synchronous version of [`max_input_tokens()`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.Embedder.max_input_tokens). ###### Returns [`int`](https://docs.python.org/3/library/functions.html#int) | [`None`](https://docs.python.org/3/library/constants.html#None) ##### count\_tokens\_sync ```python def count_tokens_sync(text: str) -> int ``` Synchronous version of [`count_tokens()`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.Embedder.count_tokens). ###### Returns [`int`](https://docs.python.org/3/library/functions.html#int) ### instrument\_embedding\_model ```python def instrument_embedding_model( model: EmbeddingModel, instrument: InstrumentationSettings | bool, ) -> EmbeddingModel ``` Instrument an embedding model with OpenTelemetry/logfire. #### Returns [`EmbeddingModel`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.EmbeddingModel) ### merge\_embedding\_settings ```python def merge_embedding_settings( base: EmbeddingSettings | None, overrides: EmbeddingSettings | None, ) -> EmbeddingSettings | None ``` Merge two sets of embedding settings, with overrides taking precedence. #### Returns [`EmbeddingSettings`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.EmbeddingSettings) | [`None`](https://docs.python.org/3/library/constants.html#None) -- Merged settings, or `None` if both inputs are `None`. #### Parameters **`base`** : [`EmbeddingSettings`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.EmbeddingSettings) | [`None`](https://docs.python.org/3/library/constants.html#None) Base settings (typically from the embedder or model). **`overrides`** : [`EmbeddingSettings`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.EmbeddingSettings) | [`None`](https://docs.python.org/3/library/constants.html#None) Settings that should override the base (typically per-call settings). ### infer\_embedding\_model ```python def infer_embedding_model( model: EmbeddingModel | KnownEmbeddingModelName | str, provider_factory: Callable[[str], Provider[Any]] = infer_provider, ) -> EmbeddingModel ``` Infer the model from the name. #### Returns [`EmbeddingModel`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.EmbeddingModel) ### KnownEmbeddingModelName Known model names that can be used with the `model` parameter of [`Embedder`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.Embedder). `KnownEmbeddingModelName` is provided as a concise way to specify an embedding model. **Default:** `TypeAliasType('KnownEmbeddingModelName', Literal['google-cloud:gemini-embedding-001', 'google-cloud:gemini-embedding-2-preview', 'google-cloud:text-embedding-005', 'google-cloud:text-multilingual-embedding-002', 'google:gemini-embedding-001', 'google:gemini-embedding-2-preview', 'openai:text-embedding-ada-002', 'openai:text-embedding-3-small', 'openai:text-embedding-3-large', 'cohere:embed-v4.0', 'cohere:embed-english-v3.0', 'cohere:embed-english-light-v3.0', 'cohere:embed-multilingual-v3.0', 'cohere:embed-multilingual-light-v3.0', 'voyageai:voyage-4-large', 'voyageai:voyage-4', 'voyageai:voyage-4-lite', 'voyageai:voyage-3-large', 'voyageai:voyage-3.5', 'voyageai:voyage-3.5-lite', 'voyageai:voyage-code-3', 'voyageai:voyage-finance-2', 'voyageai:voyage-law-2', 'voyageai:voyage-code-2', 'bedrock:amazon.titan-embed-text-v1', 'bedrock:amazon.titan-embed-text-v2:0', 'bedrock:cohere.embed-english-v3', 'bedrock:cohere.embed-multilingual-v3', 'bedrock:cohere.embed-v4:0', 'bedrock:amazon.nova-2-multimodal-embeddings-v1:0'])` ### EmbeddingModel **Bases:** `ABC` Abstract base class for embedding models. Implement this class to create a custom embedding model. For most use cases, use one of the built-in implementations: - [`OpenAIEmbeddingModel`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.openai.OpenAIEmbeddingModel) - [`CohereEmbeddingModel`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.cohere.CohereEmbeddingModel) - [`GoogleEmbeddingModel`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.google.GoogleEmbeddingModel) - [`BedrockEmbeddingModel`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.bedrock.BedrockEmbeddingModel) - [`SentenceTransformerEmbeddingModel`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.sentence_transformers.SentenceTransformerEmbeddingModel) #### Attributes ##### settings Get the default settings for this model. **Type:** [`EmbeddingSettings`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.EmbeddingSettings) | [`None`](https://docs.python.org/3/library/constants.html#None) ##### base\_url The base URL for the provider API, if available. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) ##### model\_name The name of the embedding model. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### system The embedding model provider/system identifier (e.g., 'openai', 'cohere'). **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) #### Methods ##### \_\_init\_\_ ```python def __init__(settings: EmbeddingSettings | None = None) -> None ``` Initialize the model with optional settings. ###### Returns [`None`](https://docs.python.org/3/library/constants.html#None) ###### Parameters **`settings`** : [`EmbeddingSettings`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.EmbeddingSettings) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Model-specific settings that will be used as defaults for this model. ##### embed `@abstractmethod` `@async` ```python def embed( inputs: str | Sequence[str], input_type: EmbedInputType, settings: EmbeddingSettings | None = None, ) -> EmbeddingResult ``` Generate embeddings for the given inputs. ###### Returns [`EmbeddingResult`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.EmbeddingResult) -- An [`EmbeddingResult`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.EmbeddingResult) containing [`EmbeddingResult`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.EmbeddingResult) -- the embeddings and metadata. ###### Parameters **`inputs`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str)\] A single string or sequence of strings to embed. **`input_type`** : `EmbedInputType` Whether the inputs are queries or documents. **`settings`** : [`EmbeddingSettings`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.EmbeddingSettings) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional settings to override the model's defaults. ##### prepare\_embed ```python def prepare_embed( inputs: str | Sequence[str], settings: EmbeddingSettings | None = None, ) -> tuple[list[str], EmbeddingSettings] ``` Prepare the inputs and settings for embedding. This method normalizes inputs to a list and merges settings. Subclasses should call this at the start of their `embed()` implementation. ###### Returns [`tuple`](https://docs.python.org/3/library/stdtypes.html#tuple)\[[`list`](https://docs.python.org/3/glossary.html#term-list)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str)\], [`EmbeddingSettings`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.EmbeddingSettings)\] -- A tuple of (normalized inputs list, merged settings). ###### Parameters **`inputs`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str)\] A single string or sequence of strings. **`settings`** : [`EmbeddingSettings`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.EmbeddingSettings) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional settings to merge with defaults. ##### max\_input\_tokens `@async` ```python def max_input_tokens() -> int | None ``` Get the maximum number of tokens that can be input to the model. ###### Returns [`int`](https://docs.python.org/3/library/functions.html#int) | [`None`](https://docs.python.org/3/library/constants.html#None) -- The maximum token count, or `None` if unknown. ##### count\_tokens `@async` ```python def count_tokens(text: str) -> int ``` Count the number of tokens in the given text. ###### Returns [`int`](https://docs.python.org/3/library/functions.html#int) -- The number of tokens. ###### Parameters **`text`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) The text to tokenize and count. ###### Raises - `NotImplementedError` -- If the model doesn't support token counting. - `UserError` -- If the model or tokenizer is not supported. ### EmbeddingResult The result of an embedding operation. This class contains the generated embeddings along with metadata about the operation, including the original inputs, model information, usage statistics, and timing. Example: ```python from pydantic_ai import Embedder embedder = Embedder('openai:text-embedding-3-small') async def main(): result = await embedder.embed_query('What is AI?') # Access embeddings by index print(len(result.embeddings[0])) #> 1536 # Access embeddings by original input text print(result['What is AI?'] == result.embeddings[0]) #> True # Check usage print(f'Tokens used: {result.usage.input_tokens}') #> Tokens used: 3 ``` #### Attributes ##### embeddings The computed embedding vectors, one per input text. Each embedding is a sequence of floats representing the text in vector space. **Type:** [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`float`](https://docs.python.org/3/library/functions.html#float)\]\] ##### inputs The original input texts that were embedded. **Type:** [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str)\] ##### input\_type Whether the inputs were embedded as queries or documents. **Type:** `EmbedInputType` ##### model\_name The name of the model that generated these embeddings. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### provider\_name The name of the provider (e.g., 'openai', 'cohere'). **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### timestamp When the embedding request was made. **Type:** [`datetime`](https://docs.python.org/3/library/datetime.html#module-datetime) **Default:** `field(default_factory=_now_utc)` ##### usage Token usage statistics for this request. **Type:** [`RequestUsage`](/docs/ai/api/pydantic-ai/usage/#pydantic_ai.usage.RequestUsage) **Default:** `field(default_factory=RequestUsage)` ##### provider\_details Provider-specific details from the response. **Type:** [`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `None` ##### provider\_response\_id Unique identifier for this response from the provider, if available. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `None` #### Methods ##### \_\_getitem\_\_ ```python def __getitem__(item: int | str) -> Sequence[float] ``` Get the embedding for an input by index or by the original input text. ###### Returns [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`float`](https://docs.python.org/3/library/functions.html#float)\] -- The embedding vector for the specified input. ###### Parameters **`item`** : [`int`](https://docs.python.org/3/library/functions.html#int) | [`str`](https://docs.python.org/3/library/stdtypes.html#str) Either an integer index or the original input string. ###### Raises - `IndexError` -- If the index is out of range. - `ValueError` -- If the string is not found in the inputs. ##### cost ```python def cost() -> genai_types.PriceCalculation ``` Calculate the cost of the embedding request. Uses [`genai-prices`](https://github.com/pydantic/genai-prices) for pricing data. ###### Returns `genai_types.PriceCalculation` -- A price calculation object with `total_price`, `input_price`, and other cost details. ###### Raises - `LookupError` -- If pricing data is not available for this model/provider. ### EmbedInputType The type of input to the embedding model. - `'query'`: Text that will be used as a search query - `'document'`: Text that will be stored and searched against Some embedding models optimize differently for queries vs documents. **Default:** `Literal['query', 'document']` ### EmbeddingSettings **Bases:** [`TypedDict`](https://docs.python.org/3/library/typing.html#typing.TypedDict) Common settings for configuring embedding models. These settings apply across multiple embedding model providers. Not all settings are supported by all models - check the specific model's documentation for details. Provider-specific settings classes (e.g., [`OpenAIEmbeddingSettings`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.openai.OpenAIEmbeddingSettings), [`CohereEmbeddingSettings`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.cohere.CohereEmbeddingSettings)) extend this with additional provider-prefixed options. #### Attributes ##### dimensions The number of dimensions for the output embeddings. Supported by: - OpenAI - Cohere - Google - Sentence Transformers - Bedrock - VoyageAI **Type:** [`int`](https://docs.python.org/3/library/functions.html#int) ##### truncate Whether to truncate inputs that exceed the model's context length. Defaults to `False`. If `True`, inputs that are too long will be truncated. If `False`, an error will be raised for inputs that exceed the context length. For more control over truncation, you can use [`max_input_tokens()`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.Embedder.max_input_tokens) and [`count_tokens()`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.Embedder.count_tokens) to implement your own truncation logic. Provider-specific truncation settings (e.g., `cohere_truncate`, `bedrock_cohere_truncate`) take precedence if specified. Supported by: - Cohere - Bedrock (Cohere and Nova models) - VoyageAI **Type:** [`bool`](https://docs.python.org/3/library/functions.html#bool) ##### extra\_headers Extra headers to send to the model. Supported by: - OpenAI - Cohere **Type:** [`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), [`str`](https://docs.python.org/3/library/stdtypes.html#str)\] ##### extra\_body Extra body to send to the model. Supported by: - OpenAI - Cohere **Type:** [`object`](https://docs.python.org/3/glossary.html#term-object) ### merge\_embedding\_settings ```python def merge_embedding_settings( base: EmbeddingSettings | None, overrides: EmbeddingSettings | None, ) -> EmbeddingSettings | None ``` Merge two sets of embedding settings, with overrides taking precedence. #### Returns [`EmbeddingSettings`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.EmbeddingSettings) | [`None`](https://docs.python.org/3/library/constants.html#None) -- Merged settings, or `None` if both inputs are `None`. #### Parameters **`base`** : [`EmbeddingSettings`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.EmbeddingSettings) | [`None`](https://docs.python.org/3/library/constants.html#None) Base settings (typically from the embedder or model). **`overrides`** : [`EmbeddingSettings`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.EmbeddingSettings) | [`None`](https://docs.python.org/3/library/constants.html#None) Settings that should override the base (typically per-call settings). ### OpenAIEmbeddingSettings **Bases:** [`EmbeddingSettings`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.EmbeddingSettings) Settings used for an OpenAI embedding model request. All fields from [`EmbeddingSettings`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.EmbeddingSettings) are supported. ### OpenAIEmbeddingModel **Bases:** [`EmbeddingModel`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.EmbeddingModel) OpenAI embedding model implementation. This model works with OpenAI's embeddings API and any [OpenAI-compatible providers](/docs/ai/models/openai#openai-compatible-models). Example: ```python from pydantic_ai.embeddings.openai import OpenAIEmbeddingModel from pydantic_ai.providers.openai import OpenAIProvider # Using OpenAI directly model = OpenAIEmbeddingModel('text-embedding-3-small') # Using an OpenAI-compatible provider model = OpenAIEmbeddingModel( 'text-embedding-3-small', provider=OpenAIProvider(base_url='https://my-provider.com/v1'), ) ``` #### Attributes ##### model\_name The embedding model name. **Type:** `OpenAIEmbeddingModelName` ##### system The embedding model provider. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) #### Methods ##### \_\_init\_\_ ```python def __init__( model_name: OpenAIEmbeddingModelName, provider: OpenAIEmbeddingsCompatibleProvider | Literal['openai'] | Provider[AsyncOpenAI] = 'openai', settings: EmbeddingSettings | None = None, ) ``` Initialize an OpenAI embedding model. ###### Parameters **`model_name`** : `OpenAIEmbeddingModelName` The name of the OpenAI model to use. See [OpenAI's embedding models](https://platform.openai.com/docs/guides/embeddings) for available options. **`provider`** : `OpenAIEmbeddingsCompatibleProvider` | [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['openai'\] | `Provider`\[`AsyncOpenAI`\] _Default:_ `'openai'` The provider to use for authentication and API access. Can be: - `'openai'` (default): Uses the standard OpenAI API - A provider name string (e.g., `'azure'`, `'deepseek'`) - A [`Provider`](/docs/ai/api/pydantic-ai/providers/#pydantic_ai.providers.Provider) instance for custom configuration See [OpenAI-compatible providers](/docs/ai/models/openai#openai-compatible-models) for a list of supported providers. **`settings`** : [`EmbeddingSettings`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.EmbeddingSettings) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Model-specific [`EmbeddingSettings`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.EmbeddingSettings) to use as defaults for this model. ### OpenAIEmbeddingModelName Possible OpenAI embeddings model names. See the [OpenAI embeddings documentation](https://platform.openai.com/docs/guides/embeddings) for available models. **Default:** `str | LatestOpenAIEmbeddingModelNames` ### CohereEmbeddingSettings **Bases:** [`EmbeddingSettings`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.EmbeddingSettings) Settings used for a Cohere embedding model request. All fields from [`EmbeddingSettings`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.EmbeddingSettings) are supported, plus Cohere-specific settings prefixed with `cohere_`. #### Attributes ##### cohere\_max\_tokens The maximum number of tokens to embed. **Type:** [`int`](https://docs.python.org/3/library/functions.html#int) ##### cohere\_input\_type The Cohere-specific input type for the embedding. Overrides the standard `input_type` argument. Options include: `'search_query'`, `'search_document'`, `'classification'`, `'clustering'`, and `'image'`. **Type:** `CohereEmbedInputType` ##### cohere\_truncate The truncation strategy to use: - `'NONE'` (default): Raise an error if input exceeds max tokens. - `'END'`: Truncate the end of the input text. - `'START'`: Truncate the start of the input text. Note: This setting overrides the standard `truncate` boolean setting when specified. **Type:** `V2EmbedRequestTruncate` ### CohereEmbeddingModel **Bases:** [`EmbeddingModel`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.EmbeddingModel) Cohere embedding model implementation. This model works with Cohere's embeddings API, which offers multilingual support and various model sizes. Example: ```python from pydantic_ai.embeddings.cohere import CohereEmbeddingModel model = CohereEmbeddingModel('embed-v4.0') ``` #### Attributes ##### base\_url The base URL for the provider API, if available. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### model\_name The embedding model name. **Type:** `CohereEmbeddingModelName` ##### system The embedding model provider. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) #### Methods ##### \_\_init\_\_ ```python def __init__( model_name: CohereEmbeddingModelName, provider: Literal['cohere'] | Provider[AsyncClientV2] = 'cohere', settings: EmbeddingSettings | None = None, ) ``` Initialize a Cohere embedding model. ###### Parameters **`model_name`** : `CohereEmbeddingModelName` The name of the Cohere model to use. See [Cohere Embed documentation](https://docs.cohere.com/docs/cohere-embed) for available models. **`provider`** : [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['cohere'\] | `Provider`\[`AsyncClientV2`\] _Default:_ `'cohere'` The provider to use for authentication and API access. Can be: - `'cohere'` (default): Uses the standard Cohere API - A [`CohereProvider`](/docs/ai/api/pydantic-ai/providers/#pydantic_ai.providers.cohere.CohereProvider) instance for custom configuration **`settings`** : [`EmbeddingSettings`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.EmbeddingSettings) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Model-specific [`EmbeddingSettings`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.EmbeddingSettings) to use as defaults for this model. ### LatestCohereEmbeddingModelNames Latest Cohere embeddings models. See the [Cohere Embed documentation](https://docs.cohere.com/docs/cohere-embed) for available models and their capabilities. **Default:** `Literal['embed-v4.0', 'embed-english-v3.0', 'embed-english-light-v3.0', 'embed-multilingual-v3.0', 'embed-multilingual-light-v3.0']` ### CohereEmbeddingModelName Possible Cohere embeddings model names. **Default:** `str | LatestCohereEmbeddingModelNames` ### GoogleEmbeddingSettings **Bases:** [`EmbeddingSettings`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.EmbeddingSettings) Settings used for a Google embedding model request. All fields from [`EmbeddingSettings`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.EmbeddingSettings) are supported, plus Google-specific settings prefixed with `google_`. #### Attributes ##### google\_task\_type The task type for the embedding. Overrides the automatic task type selection based on `input_type`. See [Google's task type documentation](https://ai.google.dev/gemini-api/docs/embeddings#task-types) for available options. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### google\_title Optional title for the content being embedded. Only applicable when task\_type is `RETRIEVAL_DOCUMENT`. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ### GoogleEmbeddingModel **Bases:** [`EmbeddingModel`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.EmbeddingModel) Google embedding model implementation. This model works with Google's embeddings API via the `google-genai` SDK, supporting both the Gemini API (Google AI Studio) and Google Cloud (formerly known as Vertex AI). Example: ```python from pydantic_ai.embeddings.google import GoogleEmbeddingModel from pydantic_ai.providers.google import GoogleProvider from pydantic_ai.providers.google_cloud import GoogleCloudProvider # Using the Gemini API (requires GOOGLE_API_KEY env var) model = GoogleEmbeddingModel('gemini-embedding-001', provider=GoogleProvider()) # Using Google Cloud model = GoogleEmbeddingModel( 'gemini-embedding-001', provider=GoogleCloudProvider(project='my-project', location='us-central1'), ) ``` #### Attributes ##### model\_name The embedding model name. **Type:** `GoogleEmbeddingModelName` ##### system The embedding model provider. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) #### Methods ##### \_\_init\_\_ ```python def __init__( model_name: GoogleEmbeddingModelName, provider: Literal['google', 'google-cloud'] | Provider[Client] = 'google', settings: EmbeddingSettings | None = None, ) ``` Initialize a Google embedding model. ###### Parameters **`model_name`** : `GoogleEmbeddingModelName` The name of the Google model to use. See [Google Embeddings documentation](https://ai.google.dev/gemini-api/docs/embeddings) for available models. **`provider`** : [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['google', 'google-cloud'\] | `Provider`\[`Client`\] _Default:_ `'google'` The provider to use for authentication and API access. Can be: - `'google'` (default): Uses the Gemini API (Google AI Studio) - `'google-cloud'`: Uses Google Cloud (formerly known as Vertex AI) - A [`GoogleProvider`](/docs/ai/api/pydantic-ai/providers/#pydantic_ai.providers.google.GoogleProvider) or `GoogleCloudProvider` instance for custom configuration **`settings`** : [`EmbeddingSettings`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.EmbeddingSettings) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Model-specific [`EmbeddingSettings`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.EmbeddingSettings) to use as defaults for this model. ### LatestGoogleGLAEmbeddingModelNames Latest Gemini API embedding models. See the [Google Embeddings documentation](https://ai.google.dev/gemini-api/docs/embeddings) for available models and their capabilities. **Default:** `Literal['gemini-embedding-001', 'gemini-embedding-2-preview']` ### LatestGoogleVertexEmbeddingModelNames Latest Google Cloud (formerly known as Vertex AI) embedding models. See the [Google Cloud Embeddings documentation](https://cloud.google.com/vertex-ai/generative-ai/docs/embeddings/get-text-embeddings) for available models and their capabilities. **Default:** `Literal['gemini-embedding-001', 'gemini-embedding-2-preview', 'text-embedding-005', 'text-multilingual-embedding-002']` ### LatestGoogleEmbeddingModelNames All latest Google embedding models (union of Gemini API and Google Cloud models). **Default:** `LatestGoogleGLAEmbeddingModelNames | LatestGoogleVertexEmbeddingModelNames` ### GoogleEmbeddingModelName Possible Google embeddings model names. **Default:** `str | LatestGoogleEmbeddingModelNames` ### BedrockEmbeddingSettings **Bases:** [`EmbeddingSettings`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.EmbeddingSettings) Settings used for a Bedrock embedding model request. All fields from [`EmbeddingSettings`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.EmbeddingSettings) are supported, plus Bedrock-specific settings prefixed with `bedrock_`. All settings are optional - if not specified, model defaults are used. **Note on `dimensions` parameter support:** - **Titan v1** (`amazon.titan-embed-text-v1`): Not supported (fixed: 1536) - **Titan v2** (`amazon.titan-embed-text-v2:0`): Supported (default: 1024, accepts 256/384/1024) - **Cohere v3** (`cohere.embed-english-v3`, `cohere.embed-multilingual-v3`): Not supported (fixed: 1024) - **Cohere v4** (`cohere.embed-v4:0`): Supported (default: 1536, accepts 256/512/1024/1536) - **Nova** (`amazon.nova-2-multimodal-embeddings-v1:0`): Supported (default: 3072, accepts 256/384/1024/3072) Unsupported settings are silently ignored. **Note on `truncate` parameter support:** - **Titan models** (`amazon.titan-embed-text-v1`, `amazon.titan-embed-text-v2:0`): Not supported - **Cohere models** (all versions): Supported (default: `False`, maps to `'END'` when `True`) - **Nova** (`amazon.nova-2-multimodal-embeddings-v1:0`): Supported (default: `False`, maps to `'END'` when `True`) For fine-grained truncation control, use model-specific settings: `bedrock_cohere_truncate` or `bedrock_nova_truncate`. #### Attributes ##### bedrock\_titan\_normalize Whether to normalize embedding vectors for Titan models. **Supported by:** `amazon.titan-embed-text-v2:0` (default: `True`) **Not supported by:** `amazon.titan-embed-text-v1` (silently ignored) When enabled, vectors are normalized for direct cosine similarity calculations. **Type:** [`bool`](https://docs.python.org/3/library/functions.html#bool) ##### bedrock\_cohere\_max\_tokens The maximum number of tokens to embed for Cohere models. **Supported by:** `cohere.embed-v4:0` (default: 128000) **Not supported by:** `cohere.embed-english-v3`, `cohere.embed-multilingual-v3` (silently ignored) **Type:** [`int`](https://docs.python.org/3/library/functions.html#int) ##### bedrock\_cohere\_input\_type The input type for Cohere models. **Supported by:** All Cohere models (`cohere.embed-english-v3`, `cohere.embed-multilingual-v3`, `cohere.embed-v4:0`) By default, `embed_query()` uses `'search_query'` and `embed_documents()` uses `'search_document'`. Also accepts `'classification'` or `'clustering'`. **Type:** [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['search\_document', 'search\_query', 'classification', 'clustering'\] ##### bedrock\_cohere\_truncate The truncation strategy for Cohere models. Overrides base `truncate` setting. **Supported by:** All Cohere models (`cohere.embed-english-v3`, `cohere.embed-multilingual-v3`, `cohere.embed-v4:0`) Default: `'NONE'` - `'NONE'`: Raise an error if input exceeds max tokens. - `'START'`: Truncate the start of the input. - `'END'`: Truncate the end of the input. **Type:** [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['NONE', 'START', 'END'\] ##### bedrock\_nova\_truncate The truncation strategy for Nova models. Overrides base `truncate` setting. **Supported by:** `amazon.nova-2-multimodal-embeddings-v1:0` Default: `'NONE'` - `'NONE'`: Raise an error if input exceeds max tokens. - `'START'`: Truncate the start of the input. - `'END'`: Truncate the end of the input. **Type:** [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['NONE', 'START', 'END'\] ##### bedrock\_nova\_embedding\_purpose The embedding purpose for Nova models. **Supported by:** `amazon.nova-2-multimodal-embeddings-v1:0` By default, `embed_query()` uses `'GENERIC_RETRIEVAL'` and `embed_documents()` uses `'GENERIC_INDEX'`. Also accepts `'TEXT_RETRIEVAL'`, `'CLASSIFICATION'`, or `'CLUSTERING'`. Note: Multimodal-specific purposes (`'IMAGE_RETRIEVAL'`, `'VIDEO_RETRIEVAL'`, `'DOCUMENT_RETRIEVAL'`, `'AUDIO_RETRIEVAL'`) are not supported as this embedding client only accepts text input. **Type:** [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['GENERIC\_INDEX', 'GENERIC\_RETRIEVAL', 'TEXT\_RETRIEVAL', 'CLASSIFICATION', 'CLUSTERING'\] ##### bedrock\_inference\_profile An [inference profile](https://docs.aws.amazon.com/bedrock/latest/userguide/inference-profiles.html) ARN to use as the `modelId` in API requests. When set, this value is used as the `modelId` in `invoke_model` API calls instead of the base `model_name`. This allows you to pass the base model name (e.g. `'amazon.titan-embed-text-v2:0'`) as `model_name` for detecting model capabilities, while routing requests through an inference profile for cost tracking or cross-region inference. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### bedrock\_max\_concurrency Maximum number of concurrent requests for models that don't support batch embedding. **Applies to:** `amazon.titan-embed-text-v1`, `amazon.titan-embed-text-v2:0`, `amazon.nova-2-multimodal-embeddings-v1:0` When embedding multiple texts with models that only support single-text requests, this controls how many requests run in parallel. Defaults to 5. **Type:** [`int`](https://docs.python.org/3/library/functions.html#int) ### BedrockEmbeddingModel **Bases:** [`EmbeddingModel`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.EmbeddingModel) Bedrock embedding model implementation. This model works with AWS Bedrock's embedding models including Amazon Titan Embeddings and Cohere Embed models. Example: ```python from pydantic_ai.embeddings.bedrock import BedrockEmbeddingModel from pydantic_ai.providers.bedrock import BedrockProvider # Using default AWS credentials model = BedrockEmbeddingModel('amazon.titan-embed-text-v2:0') # Using explicit credentials model = BedrockEmbeddingModel( 'cohere.embed-english-v3', provider=BedrockProvider( region_name='us-east-1', aws_access_key_id='...', aws_secret_access_key='...', ), ) ``` #### Attributes ##### base\_url The base URL for the provider API. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### model\_name The embedding model name. **Type:** `BedrockEmbeddingModelName` ##### system The embedding model provider. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) #### Methods ##### \_\_init\_\_ ```python def __init__( model_name: BedrockEmbeddingModelName, provider: Literal['bedrock'] | Provider[BaseClient] = 'bedrock', settings: EmbeddingSettings | None = None, ) ``` Initialize a Bedrock embedding model. ###### Parameters **`model_name`** : `BedrockEmbeddingModelName` The name of the Bedrock embedding model to use. See [Bedrock embedding models](https://docs.aws.amazon.com/bedrock/latest/userguide/models-supported.html) for available options. **`provider`** : [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['bedrock'\] | `Provider`\[`BaseClient`\] _Default:_ `'bedrock'` The provider to use for authentication and API access. Can be: - `'bedrock'` (default): Uses default AWS credentials - A [`BedrockProvider`](/docs/ai/api/pydantic-ai/providers/#pydantic_ai.providers.bedrock.BedrockProvider) instance for custom configuration **`settings`** : [`EmbeddingSettings`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.EmbeddingSettings) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Model-specific [`EmbeddingSettings`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.EmbeddingSettings) to use as defaults for this model. ##### max\_input\_tokens `@async` ```python def max_input_tokens() -> int | None ``` Get the maximum number of tokens that can be input to the model. ###### Returns [`int`](https://docs.python.org/3/library/functions.html#int) | [`None`](https://docs.python.org/3/library/constants.html#None) ### LatestBedrockEmbeddingModelNames Latest Bedrock embedding model names. See [the Bedrock docs](https://docs.aws.amazon.com/bedrock/latest/userguide/models-supported.html) for available embedding models. **Default:** `Literal['amazon.titan-embed-text-v1', 'amazon.titan-embed-text-v2:0', 'cohere.embed-english-v3', 'cohere.embed-multilingual-v3', 'cohere.embed-v4:0', 'amazon.nova-2-multimodal-embeddings-v1:0']` ### BedrockEmbeddingModelName Possible Bedrock embedding model names. **Default:** `str | LatestBedrockEmbeddingModelNames` ### VoyageAIEmbeddingSettings **Bases:** [`EmbeddingSettings`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.EmbeddingSettings) Settings used for a VoyageAI embedding model request. All fields from [`EmbeddingSettings`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.EmbeddingSettings) are supported, plus VoyageAI-specific settings prefixed with `voyageai_`. #### Attributes ##### voyageai\_input\_type The VoyageAI-specific input type for the embedding. Overrides the standard `input_type` argument. Options include: `'query'`, `'document'`, or `'none'` for direct embedding without prefix. **Type:** `VoyageAIEmbedInputType` ### VoyageAIEmbeddingModel **Bases:** [`EmbeddingModel`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.EmbeddingModel) VoyageAI embedding model implementation. VoyageAI provides state-of-the-art embedding models optimized for retrieval, with specialized models for code, finance, and legal domains. Example: ```python from pydantic_ai.embeddings.voyageai import VoyageAIEmbeddingModel model = VoyageAIEmbeddingModel('voyage-3.5') ``` #### Attributes ##### base\_url The base URL for the provider API. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### model\_name The embedding model name. **Type:** `VoyageAIEmbeddingModelName` ##### system The embedding model provider. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) #### Methods ##### \_\_init\_\_ ```python def __init__( model_name: VoyageAIEmbeddingModelName, provider: Literal['voyageai'] | Provider[AsyncClient] = 'voyageai', settings: EmbeddingSettings | None = None, ) ``` Initialize a VoyageAI embedding model. ###### Parameters **`model_name`** : `VoyageAIEmbeddingModelName` The name of the VoyageAI model to use. See [VoyageAI models](https://docs.voyageai.com/docs/embeddings) for available options. **`provider`** : [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['voyageai'\] | `Provider`\[`AsyncClient`\] _Default:_ `'voyageai'` The provider to use for authentication and API access. Can be: - `'voyageai'` (default): Uses the standard VoyageAI API - A [`VoyageAIProvider`](/docs/ai/api/pydantic-ai/providers/#pydantic_ai.providers.voyageai.VoyageAIProvider) instance for custom configuration **`settings`** : [`EmbeddingSettings`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.EmbeddingSettings) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Model-specific [`EmbeddingSettings`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.EmbeddingSettings) to use as defaults for this model. ### LatestVoyageAIEmbeddingModelNames Latest VoyageAI embedding models. See [VoyageAI Embeddings](https://docs.voyageai.com/docs/embeddings) for available models and their capabilities. **Default:** `Literal['voyage-4-large', 'voyage-4', 'voyage-4-lite', 'voyage-3-large', 'voyage-3.5', 'voyage-3.5-lite', 'voyage-code-3', 'voyage-finance-2', 'voyage-law-2', 'voyage-code-2']` ### VoyageAIEmbeddingModelName Possible VoyageAI embedding model names. **Default:** `str | LatestVoyageAIEmbeddingModelNames` ### VoyageAIEmbedInputType VoyageAI embedding input types. - `'query'`: For search queries; prepends retrieval-optimized prefix. - `'document'`: For documents; prepends document retrieval prefix. - `'none'`: Direct embedding without any prefix. **Default:** `Literal['query', 'document', 'none']` ### SentenceTransformersEmbeddingSettings **Bases:** [`EmbeddingSettings`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.EmbeddingSettings) Settings used for a Sentence-Transformers embedding model request. All fields from [`EmbeddingSettings`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.EmbeddingSettings) are supported, plus Sentence-Transformers-specific settings prefixed with `sentence_transformers_`. #### Attributes ##### sentence\_transformers\_device Device to run inference on. Examples: `'cpu'`, `'cuda'`, `'cuda:0'`, `'mps'` (Apple Silicon). **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### sentence\_transformers\_normalize\_embeddings Whether to L2-normalize embeddings. When `True`, all embeddings will have unit length, which is useful for cosine similarity calculations. **Type:** [`bool`](https://docs.python.org/3/library/functions.html#bool) ##### sentence\_transformers\_batch\_size Batch size to use during encoding. Larger batches may be faster but require more memory. **Type:** [`int`](https://docs.python.org/3/library/functions.html#int) ### SentenceTransformerEmbeddingModel **Bases:** [`EmbeddingModel`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.EmbeddingModel) Local embedding model using the `sentence-transformers` library. This model runs embeddings locally on your machine, which is useful for: - Privacy-sensitive applications where data shouldn't leave your infrastructure - Reducing API costs for high-volume embedding workloads - Offline or air-gapped environments Models are downloaded from Hugging Face on first use. See the [Sentence-Transformers documentation](https://www.sbert.net/docs/sentence_transformer/pretrained_models.html) for available models. Example: ```python from sentence_transformers import SentenceTransformer from pydantic_ai.embeddings.sentence_transformers import ( SentenceTransformerEmbeddingModel, ) # Using a model name (downloads from Hugging Face) model = SentenceTransformerEmbeddingModel('sentence-transformers/all-MiniLM-L6-v2') # Using an existing SentenceTransformer instance st_model = SentenceTransformer('Qwen/Qwen3-Embedding-0.6B') model = SentenceTransformerEmbeddingModel(st_model) ``` #### Attributes ##### base\_url No base URL -- runs locally. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) ##### model\_name The embedding model name. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### system The embedding model provider/system identifier. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) #### Methods ##### \_\_init\_\_ ```python def __init__( model: SentenceTransformer | str, settings: EmbeddingSettings | None = None, ) -> None ``` Initialize a Sentence-Transformers embedding model. ###### Returns [`None`](https://docs.python.org/3/library/constants.html#None) ###### Parameters **`model`** : `SentenceTransformer` | [`str`](https://docs.python.org/3/library/stdtypes.html#str) The model to use. Can be: - A model name from Hugging Face (e.g., `'sentence-transformers/all-MiniLM-L6-v2'`) - A local path to a saved model - An existing `SentenceTransformer` instance **`settings`** : [`EmbeddingSettings`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.EmbeddingSettings) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Model-specific [`SentenceTransformersEmbeddingSettings`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.sentence_transformers.SentenceTransformersEmbeddingSettings) to use as defaults for this model. ### TestEmbeddingModel **Bases:** [`EmbeddingModel`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.EmbeddingModel) A mock embedding model for testing. This model returns deterministic embeddings (all 1.0 values) and tracks the settings used in the last call via the `last_settings` attribute. Example: ```python from pydantic_ai import Embedder from pydantic_ai.embeddings import TestEmbeddingModel test_model = TestEmbeddingModel() embedder = Embedder('openai:text-embedding-3-small') async def main(): with embedder.override(model=test_model): await embedder.embed_query('test') assert test_model.last_settings is not None ``` #### Attributes ##### last\_settings The settings used in the most recent embed call. **Type:** [`EmbeddingSettings`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.EmbeddingSettings) | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `None` ##### model\_name The embedding model name. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### system The embedding model provider. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) #### Methods ##### \_\_init\_\_ ```python def __init__( model_name: str = 'test', provider_name: str = 'test', dimensions: int = 8, settings: EmbeddingSettings | None = None, ) ``` Initialize the test embedding model. ###### Parameters **`model_name`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) _Default:_ `'test'` The model name to report in results. **`provider_name`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) _Default:_ `'test'` The provider name to report in results. **`dimensions`** : [`int`](https://docs.python.org/3/library/functions.html#int) _Default:_ `8` The number of dimensions for the generated embeddings. **`settings`** : [`EmbeddingSettings`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.EmbeddingSettings) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional default settings for the model. ### WrapperEmbeddingModel **Bases:** [`EmbeddingModel`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.EmbeddingModel) Base class for embedding models that wrap another model. Use this as a base class to create custom embedding model wrappers that modify behavior (e.g., caching, logging, rate limiting) while delegating to an underlying model. By default, all methods are passed through to the wrapped model. Override specific methods to customize behavior. #### Attributes ##### wrapped The underlying embedding model being wrapped. **Type:** [`EmbeddingModel`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.EmbeddingModel) **Default:** `infer_embedding_model(wrapped) if isinstance(wrapped, str) else wrapped` ##### settings Get the settings from the wrapped embedding model. **Type:** [`EmbeddingSettings`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.EmbeddingSettings) | [`None`](https://docs.python.org/3/library/constants.html#None) #### Methods ##### \_\_init\_\_ ```python def __init__(wrapped: EmbeddingModel | str) ``` Initialize the wrapper with an embedding model. ###### Parameters **`wrapped`** : [`EmbeddingModel`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.EmbeddingModel) | [`str`](https://docs.python.org/3/library/stdtypes.html#str) The model to wrap. Can be an [`EmbeddingModel`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.EmbeddingModel) instance or a model name string (e.g., `'openai:text-embedding-3-small'`). ### InstrumentedEmbeddingModel **Bases:** `WrapperEmbeddingModel` Embedding model which wraps another model so that requests are instrumented with OpenTelemetry. See the [Debugging and Monitoring guide](https://ai.pydantic.dev/logfire/) for more info. #### Attributes ##### instrumentation\_settings Instrumentation settings for this model. **Type:** [`InstrumentationSettings`](/docs/ai/api/models/instrumented/#pydantic_ai.models.instrumented.InstrumentationSettings) **Default:** `options or InstrumentationSettings()` ### instrument\_embedding\_model ```python def instrument_embedding_model( model: EmbeddingModel, instrument: InstrumentationSettings | bool, ) -> EmbeddingModel ``` Instrument an embedding model with OpenTelemetry/logfire. #### Returns [`EmbeddingModel`](/docs/ai/api/pydantic-ai/embeddings/#pydantic_ai.embeddings.EmbeddingModel) --- # [pydantic_ai.exceptions](https://pydantic.dev/docs/ai/api/pydantic-ai/exceptions/) # pydantic\_ai.exceptions ### ModelRetry **Bases:** [`Exception`](https://docs.python.org/3/library/exceptions.html#Exception) Exception to raise to request a model retry. Can be raised from tool functions, output validators, and capability hooks (such as `after_model_request`, `after_tool_execute`, etc.) to send a retry prompt back to the model asking it to try again. #### Attributes ##### message The message to return to the model. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) **Default:** `message` #### Methods ##### \_\_get\_pydantic\_core\_schema\_\_ `@classmethod` ```python def __get_pydantic_core_schema__(cls, _: Any, __: Any) -> core_schema.CoreSchema ``` Pydantic core schema to allow `ModelRetry` to be (de)serialized. ###### Returns `core_schema.CoreSchema` ### CallDeferred **Bases:** [`Exception`](https://docs.python.org/3/library/exceptions.html#Exception) Exception to raise when a tool call should be deferred. See [tools docs](/docs/ai/tools-toolsets/deferred-tools#deferred-tools) for more information. #### Constructor Parameters **`metadata`** : [`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional dictionary of metadata to attach to the deferred tool call. This metadata will be available in `DeferredToolRequests.metadata` keyed by `tool_call_id`. ### ApprovalRequired **Bases:** [`Exception`](https://docs.python.org/3/library/exceptions.html#Exception) Exception to raise when a tool call requires human-in-the-loop approval. See [tools docs](/docs/ai/tools-toolsets/deferred-tools#human-in-the-loop-tool-approval) for more information. #### Constructor Parameters **`metadata`** : [`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional dictionary of metadata to attach to the deferred tool call. This metadata will be available in `DeferredToolRequests.metadata` keyed by `tool_call_id`. ### SkipModelRequest **Bases:** [`Exception`](https://docs.python.org/3/library/exceptions.html#Exception) Exception to raise in before/wrap model request hooks to skip the model call. The provided response will be used instead of calling the model. Note: when raised in `before_model_request`, any message history modifications made by earlier capabilities in that hook will not be persisted to the agent's message history, since the request preparation is aborted. ### SkipToolValidation **Bases:** [`Exception`](https://docs.python.org/3/library/exceptions.html#Exception) Exception to raise in before/wrap tool validate hooks to skip validation. The provided args will be used as the validated arguments. ### SkipToolExecution **Bases:** [`Exception`](https://docs.python.org/3/library/exceptions.html#Exception) Exception to raise in before/wrap tool execute hooks to skip execution. The provided result will be used as the tool result. ### UserError **Bases:** [`RuntimeError`](https://docs.python.org/3/library/exceptions.html#RuntimeError) Error caused by a usage mistake by the application developer -- You! #### Attributes ##### message Description of the mistake. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) **Default:** `message` ### AgentRunError **Bases:** [`RuntimeError`](https://docs.python.org/3/library/exceptions.html#RuntimeError) Base class for errors occurring during an agent run. #### Attributes ##### message The error message. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) **Default:** `message` ### UsageLimitExceeded **Bases:** [`AgentRunError`](/docs/ai/api/pydantic-ai/exceptions/#pydantic_ai.exceptions.AgentRunError) Error raised when a Model's usage exceeds the specified limits. ### ConcurrencyLimitExceeded **Bases:** [`AgentRunError`](/docs/ai/api/pydantic-ai/exceptions/#pydantic_ai.exceptions.AgentRunError) Error raised when the concurrency queue depth exceeds max\_queued. ### UnexpectedModelBehavior **Bases:** [`AgentRunError`](/docs/ai/api/pydantic-ai/exceptions/#pydantic_ai.exceptions.AgentRunError) Error caused by unexpected Model behavior, e.g. an unexpected response code. #### Attributes ##### message Description of the unexpected behavior. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) **Default:** `message` ##### body The body of the response, if available. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `json.dumps(json.loads(body), indent=2)` ### ContentFilterError **Bases:** [`UnexpectedModelBehavior`](/docs/ai/api/pydantic-ai/exceptions/#pydantic_ai.exceptions.UnexpectedModelBehavior) Raised when content filtering is triggered by the model provider resulting in an empty response. ### ModelAPIError **Bases:** [`AgentRunError`](/docs/ai/api/pydantic-ai/exceptions/#pydantic_ai.exceptions.AgentRunError) Raised when a model provider API request fails. #### Attributes ##### model\_name The name of the model associated with the error. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) **Default:** `model_name` ### ModelHTTPError **Bases:** [`ModelAPIError`](/docs/ai/api/pydantic-ai/exceptions/#pydantic_ai.exceptions.ModelAPIError) Raised when a model provider response has a status code of 4xx or 5xx. #### Attributes ##### status\_code The HTTP status code returned by the API. **Type:** [`int`](https://docs.python.org/3/library/functions.html#int) **Default:** `status_code` ##### body The body of the response, if available. **Type:** [`object`](https://docs.python.org/3/glossary.html#term-object) | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `body` ### FallbackExceptionGroup **Bases:** `ExceptionGroup[Any]` A group of exceptions that can be raised when all fallback models fail. ### ToolRetryError **Bases:** [`Exception`](https://docs.python.org/3/library/exceptions.html#Exception) Exception used to signal a `ToolRetry` message should be returned to the LLM. ### IncompleteToolCall **Bases:** [`UnexpectedModelBehavior`](/docs/ai/api/pydantic-ai/exceptions/#pydantic_ai.exceptions.UnexpectedModelBehavior) Error raised when a model stops due to token limit while emitting a tool call. --- # [pydantic_ai.ext](https://pydantic.dev/docs/ai/api/pydantic-ai/ext/) # pydantic\_ai.ext ### LangChainToolset **Bases:** [`FunctionToolset`](/docs/ai/api/pydantic-ai/toolsets/#pydantic_ai.toolsets.FunctionToolset) A toolset that wraps LangChain tools. ### tool\_from\_langchain ```python def tool_from_langchain(langchain_tool: LangChainTool) -> Tool ``` Creates a Pydantic AI tool proxy from a LangChain tool. #### Returns [`Tool`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.Tool) -- A Pydantic AI tool that corresponds to the LangChain tool. #### Parameters **`langchain_tool`** : `LangChainTool` The LangChain tool to wrap. ### ACIToolset **Bases:** [`FunctionToolset`](/docs/ai/api/pydantic-ai/toolsets/#pydantic_ai.toolsets.FunctionToolset) A toolset that wraps ACI.dev tools. ### tool\_from\_aci ```python def tool_from_aci(aci_function: str, linked_account_owner_id: str) -> Tool ``` Creates a Pydantic AI tool proxy from an ACI.dev function. #### Returns [`Tool`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.Tool) -- A Pydantic AI tool that corresponds to the ACI.dev tool. #### Parameters **`aci_function`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) The ACI.dev function to wrap. **`linked_account_owner_id`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) The ACI user ID to execute the function on behalf of. --- # [pydantic_ai.format_prompt](https://pydantic.dev/docs/ai/api/pydantic-ai/format_prompt/) # pydantic\_ai.format\_prompt ### format\_as\_xml ```python def format_as_xml( obj: Any, root_tag: str | None = None, item_tag: str = 'item', none_str: str = 'null', indent: str | None = ' ', include_field_info: Literal['once'] | bool = False, ) -> str ``` Format a Python object as XML. This is useful since LLMs often find it easier to read semi-structured data (e.g. examples) as XML, rather than JSON etc. Supports: `str`, `bytes`, `bytearray`, `bool`, `int`, `float`, `Decimal`, `date`, `datetime`, `time`, `timedelta`, `UUID`, `Enum`, `Mapping`, `Iterable`, `dataclass`, and `BaseModel`. Example: format\_as\_xml\_example.py ```python from pydantic_ai import format_as_xml print(format_as_xml({'name': 'John', 'height': 6, 'weight': 200}, root_tag='user')) ''' John 6 200 ''' ``` #### Returns [`str`](https://docs.python.org/3/library/stdtypes.html#str) -- XML representation of the object. #### Parameters **`obj`** : [`Any`](https://docs.python.org/3/library/typing.html#typing.Any) Python Object to serialize to XML. **`root_tag`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Outer tag to wrap the XML in, use `None` to omit the outer tag. **`item_tag`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) _Default:_ `'item'` Tag to use for each item in an iterable (e.g. list), this is overridden by the class name for dataclasses and Pydantic models. **`none_str`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) _Default:_ `'null'` String to use for `None` values. **`indent`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `' '` Indentation string to use for pretty printing. **`include_field_info`** : [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['once'\] | [`bool`](https://docs.python.org/3/library/functions.html#bool) _Default:_ `False` Whether to include attributes like Pydantic `Field` attributes and dataclasses `field()` `metadata` as XML attributes. In both cases the allowed `Field` attributes and `field()` metadata keys are `title` and `description`. If a field is repeated in the data (e.g. in a list) by setting `once` the attributes are included only in the first occurrence of an XML element relative to the same field. --- # [pydantic_ai.function_signature](https://pydantic.dev/docs/ai/api/pydantic-ai/function_signature/) # pydantic\_ai.function\_signature Generate function signatures from functions and JSON schemas. This module provides utilities to represent tool definitions as human-readable function signatures, which LLMs can understand more easily than raw JSON schemas. Used by code mode to present tools as callable functions. ### SimpleTypeExpr A simple named type like `str`, `int`, `Any`, `None`. ### LiteralTypeExpr A Literal type expression like `Literal['a', 'b']` or `Literal[42]`. ### GenericTypeExpr A generic type expression like `list[User]`, `dict[str, User]`, `tuple[int, str]`. ### UnionTypeExpr A union type expression like `User | None`, `str | int`. ### TypeFieldSignature A single field in a TypedDict-style type definition. #### Methods ##### \_\_str\_\_ ```python def __str__() -> str ``` Render this field as a line in a TypedDict class body. ###### Returns [`str`](https://docs.python.org/3/library/stdtypes.html#str) ### TypeSignature A TypedDict-style class definition with named fields. #### Attributes ##### display\_name The type name, with tool-name prefix applied if rendering context is set. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) #### Methods ##### \_\_str\_\_ ```python def __str__() -> str ``` Return the type name (for use in type expressions like `def foo(x: User)`). ###### Returns [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### render\_definition ```python def render_definition( owner_name: str | None = None, conflicting_type_names: frozenset[str] = frozenset(), ) -> str ``` Render the full TypedDict class definition. ###### Returns [`str`](https://docs.python.org/3/library/stdtypes.html#str) ###### Parameters **`owner_name`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` The owning tool name, used to build prefixed type names for conflicting types (e.g. `get_user_Address`). **`conflicting_type_names`** : [`frozenset`](https://docs.python.org/3/library/stdtypes.html#frozenset)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str)\] _Default:_ `frozenset()` Set of type names that need tool-name prefixes (from `get_conflicting_type_names`). Only effective when `owner_name` is also provided. ##### structurally\_equal ```python def structurally_equal(other: TypeSignature) -> bool ``` Compare two TypeSignatures structurally, ignoring descriptions. ###### Returns [`bool`](https://docs.python.org/3/library/functions.html#bool) ### FunctionParam A single parameter in a function signature. #### Methods ##### \_\_str\_\_ ```python def __str__() -> str ``` Render this parameter as a function parameter string. ###### Returns [`str`](https://docs.python.org/3/library/stdtypes.html#str) ### FunctionSignature Function signature shape with referenced type definitions. This class holds the structural data (params, return type, referenced types) needed to render a function signature. Name and description can be overridden at render time (e.g. from a `ToolDefinition`). #### Attributes ##### params Function parameters, all rendered as keyword-only (JSON schema doesn't distinguish positional/keyword). **Type:** [`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), `FunctionParam`\] **Default:** `field(default_factory=(dict[str, FunctionParam]))` ##### return\_type The return type expression. **Type:** `TypeExpr` ##### referenced\_types TypedDict class definitions needed by the signature. **Type:** [`list`](https://docs.python.org/3/glossary.html#term-list)\[`TypeSignature`\] **Default:** `field(default_factory=(list[TypeSignature]))` ##### is\_async Whether the underlying function is async. **Type:** [`bool`](https://docs.python.org/3/library/functions.html#bool) **Default:** `False` #### Methods ##### render ```python def render( body: str, name: str | None = None, description: str | None = None, is_async: bool | None = None, conflicting_type_names: frozenset[str] = frozenset(), ) -> str ``` Render the signature with a specific body. Sets `_type_name_overrides` so that dedup-prefixed types resolve correctly during rendering. ###### Returns [`str`](https://docs.python.org/3/library/stdtypes.html#str) ###### Parameters **`body`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) The function body (e.g. `'...'` or `'return await tool()'`). **`name`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` The function name (also used for dedup prefix resolution). Falls back to `self.name`. **`description`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional docstring to include. Falls back to `self.description`. **`is_async`** : [`bool`](https://docs.python.org/3/library/functions.html#bool) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Override async rendering. If `None`, uses `self.is_async`. **`conflicting_type_names`** : [`frozenset`](https://docs.python.org/3/library/stdtypes.html#frozenset)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str)\] _Default:_ `frozenset()` Set of type names that need tool-name prefixes (from `get_conflicting_type_names`). ##### from\_schema `@classmethod` ```python def from_schema( cls, name: str, parameters_schema: dict[str, Any], return_schema: dict[str, Any] | None = None, ) -> FunctionSignature ``` Build a FunctionSignature from JSON schemas. `name` is stored on the resulting signature and also used for generating fallback type names (e.g. `GetUserAddress`) when the schema has no `title`. Parameter and return schemas are processed independently -- each resolves `$ref`s against its own `$defs`. Name collisions between parameter and return types (e.g. both define a `User` `$def` with different structures) are handled by `get_conflicting_type_names` at a later stage. ###### Returns `FunctionSignature` ##### get\_conflicting\_type\_names `@staticmethod` ```python def get_conflicting_type_names(signatures: list[FunctionSignature]) -> frozenset[str] ``` Identify TypedDict name conflicts across multiple tool signatures. Each signature keeps all its referenced types (so it remains self-contained), but identical types (same name and structure) are unified to the same object instance. Returns the set of type names that have conflicts (same name, different structure) and need tool-name prefixes at render time. Pass this set to `FunctionSignature.render(conflicting_type_names=...)`. Use `collect_unique_referenced_types()` when rendering to emit each definition once. ###### Returns [`frozenset`](https://docs.python.org/3/library/stdtypes.html#frozenset)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str)\] ##### collect\_unique\_referenced\_types `@staticmethod` ```python def collect_unique_referenced_types( signatures: list[FunctionSignature], ) -> list[TypeSignature] ``` Collect unique TypeSignature objects from signatures, deduplicating by identity. ###### Returns [`list`](https://docs.python.org/3/glossary.html#term-list)\[`TypeSignature`\] ##### render\_type\_definitions `@staticmethod` ```python def render_type_definitions( signatures: list[FunctionSignature], conflicting_type_names: frozenset[str], ) -> list[str] ``` Render unique TypedDict definitions for a set of function signatures. For types whose names conflict across signatures (as identified by `get_conflicting_type_names`), each definition is rendered with a tool-name prefix (e.g. `get_user_Address`). ###### Returns [`list`](https://docs.python.org/3/glossary.html#term-list)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str)\] -- A list of rendered TypedDict class definitions as strings. ###### Parameters **`signatures`** : [`list`](https://docs.python.org/3/glossary.html#term-list)\[`FunctionSignature`\] The function signatures (after `get_conflicting_type_names`). **`conflicting_type_names`** : [`frozenset`](https://docs.python.org/3/library/stdtypes.html#frozenset)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str)\] The set returned by `get_conflicting_type_names`. ### TypeExpr A type expression node in the signature's type tree. **Type:** [`TypeAlias`](https://docs.python.org/3/library/typing.html#typing.TypeAlias) **Default:** `'TypeSignature | SimpleTypeExpr | LiteralTypeExpr | GenericTypeExpr | UnionTypeExpr'` --- # [pydantic_ai.mcp](https://pydantic.dev/docs/ai/api/pydantic-ai/mcp/) # pydantic\_ai.mcp ### MCPError **Bases:** [`RuntimeError`](https://docs.python.org/3/library/exceptions.html#RuntimeError) Raised when an MCP server returns an error response. This exception wraps error responses from MCP servers, following the ErrorData schema from the MCP specification. #### Attributes ##### message The error message. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) **Default:** `message` ##### code The error code returned by the server. **Type:** [`int`](https://docs.python.org/3/library/functions.html#int) **Default:** `code` ##### data Additional information about the error, if provided by the server. **Type:** [`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `data` #### Methods ##### from\_mcp\_sdk `@classmethod` ```python def from_mcp_sdk(cls, error: mcp_exceptions.McpError) -> MCPError ``` Create an MCPError from an MCP SDK McpError. ###### Returns `MCPError` ###### Parameters **`error`** : `mcp_exceptions.McpError` An McpError from the MCP SDK. ### ResourceAnnotations Additional properties describing MCP entities. See the [resource annotations in the MCP specification](https://modelcontextprotocol.io/specification/2025-06-18/server/resources#annotations). #### Attributes ##### audience Intended audience for this entity. **Type:** [`list`](https://docs.python.org/3/glossary.html#term-list)\[`mcp_types.Role`\] | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `None` ##### priority Priority level for this entity, ranging from 0.0 to 1.0. **Type:** [`Annotated`](https://docs.python.org/3/library/typing.html#typing.Annotated)\[[`float`](https://docs.python.org/3/library/functions.html#float), `Field`(`ge`\=0.0, `le`\=1.0)\] | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `None` #### Methods ##### from\_mcp\_sdk `@classmethod` ```python def from_mcp_sdk(cls, mcp_annotations: mcp_types.Annotations) -> ResourceAnnotations ``` Convert from MCP SDK Annotations to ResourceAnnotations. ###### Returns `ResourceAnnotations` ###### Parameters **`mcp_annotations`** : `mcp_types.Annotations` The MCP SDK annotations object. ### BaseResource **Bases:** `ABC` Base class for MCP resources. #### Attributes ##### name The programmatic name of the resource. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### title Human-readable title for UI contexts. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `None` ##### description A description of what this resource represents. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `None` ##### mime\_type The MIME type of the resource, if known. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `None` ##### annotations Optional annotations for the resource. **Type:** `ResourceAnnotations` | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `None` ##### metadata Optional metadata for the resource. **Type:** [`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `None` ### Resource **Bases:** `BaseResource` A resource that can be read from an MCP server. See the [resources in the MCP specification](https://modelcontextprotocol.io/specification/2025-06-18/server/resources). #### Attributes ##### uri The URI of the resource. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### size The size of the raw resource content in bytes (before base64 encoding), if known. **Type:** [`int`](https://docs.python.org/3/library/functions.html#int) | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `None` #### Methods ##### from\_mcp\_sdk `@classmethod` ```python def from_mcp_sdk(cls, mcp_resource: mcp_types.Resource) -> Resource ``` Convert from MCP SDK Resource to PydanticAI Resource. ###### Returns `Resource` ###### Parameters **`mcp_resource`** : `mcp_types.Resource` The MCP SDK Resource object. ### ResourceTemplate **Bases:** `BaseResource` A template for parameterized resources on an MCP server. See the [resource templates in the MCP specification](https://modelcontextprotocol.io/specification/2025-06-18/server/resources#resource-templates). #### Attributes ##### uri\_template URI template (RFC 6570) for constructing resource URIs. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) #### Methods ##### from\_mcp\_sdk `@classmethod` ```python def from_mcp_sdk(cls, mcp_template: mcp_types.ResourceTemplate) -> ResourceTemplate ``` Convert from MCP SDK ResourceTemplate to PydanticAI ResourceTemplate. ###### Returns `ResourceTemplate` ###### Parameters **`mcp_template`** : `mcp_types.ResourceTemplate` The MCP SDK ResourceTemplate object. ### ServerCapabilities Capabilities that an MCP server supports. #### Attributes ##### experimental Experimental, non-standard capabilities that the server supports. **Type:** [`list`](https://docs.python.org/3/glossary.html#term-list)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str)\] | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `None` ##### logging Whether the server supports sending log messages to the client. **Type:** [`bool`](https://docs.python.org/3/library/functions.html#bool) **Default:** `False` ##### prompts Whether the server offers any prompt templates. **Type:** [`bool`](https://docs.python.org/3/library/functions.html#bool) **Default:** `False` ##### prompts\_list\_changed Whether the server will emit notifications when the list of prompts changes. **Type:** [`bool`](https://docs.python.org/3/library/functions.html#bool) **Default:** `False` ##### resources Whether the server offers any resources to read. **Type:** [`bool`](https://docs.python.org/3/library/functions.html#bool) **Default:** `False` ##### resources\_list\_changed Whether the server will emit notifications when the list of resources changes. **Type:** [`bool`](https://docs.python.org/3/library/functions.html#bool) **Default:** `False` ##### tools Whether the server offers any tools to call. **Type:** [`bool`](https://docs.python.org/3/library/functions.html#bool) **Default:** `False` ##### tools\_list\_changed Whether the server will emit notifications when the list of tools changes. **Type:** [`bool`](https://docs.python.org/3/library/functions.html#bool) **Default:** `False` ##### completions Whether the server offers autocompletion suggestions for prompts and resources. **Type:** [`bool`](https://docs.python.org/3/library/functions.html#bool) **Default:** `False` #### Methods ##### from\_mcp\_sdk `@classmethod` ```python def from_mcp_sdk( cls, mcp_capabilities: mcp_types.ServerCapabilities, ) -> ServerCapabilities ``` Convert from MCP SDK ServerCapabilities to PydanticAI ServerCapabilities. ###### Returns `ServerCapabilities` ###### Parameters **`mcp_capabilities`** : `mcp_types.ServerCapabilities` The MCP SDK ServerCapabilities object. ### MCPServer **Bases:** `AbstractToolset[Any]`, `ABC` Base class for attaching agents to MCP servers. See [https://modelcontextprotocol.io](https://modelcontextprotocol.io) for more information. Deprecated This class hierarchy (`MCPServer`, `MCPServerStdio`, `MCPServerSSE`, `MCPServerStreamableHTTP`, `MCPServerHTTP`) is deprecated in favor of [`MCPToolset`](/docs/ai/api/pydantic-ai/mcp/#pydantic_ai.mcp.MCPToolset), which is built on the more capable FastMCP client and supports the full MCP protocol. The concrete subclasses will be removed in v2. #### Attributes ##### tool\_prefix A prefix to add to all tools that are registered with the server. If not empty, will include a trailing underscore(`_`). e.g. if `tool_prefix='foo'`, then a tool named `bar` will be registered as `foo_bar` **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `tool_prefix` ##### log\_level The log level to set when connecting to the server, if any. See [https://modelcontextprotocol.io/specification/2025-03-26/server/utilities/logging#logging](https://modelcontextprotocol.io/specification/2025-03-26/server/utilities/logging#logging) for more details. If `None`, no log level will be set. **Type:** `mcp_types.LoggingLevel` | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `log_level` ##### log\_handler A handler for logging messages from the server. **Type:** `LoggingFnT` | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `log_handler` ##### timeout The timeout in seconds to wait for the client to initialize. **Type:** [`float`](https://docs.python.org/3/library/functions.html#float) **Default:** `timeout` ##### read\_timeout Maximum time in seconds to wait for new messages before timing out. This timeout applies to the long-lived connection after it's established. If no new messages are received within this time, the connection will be considered stale and may be closed. Defaults to 5 minutes (300 seconds). **Type:** [`float`](https://docs.python.org/3/library/functions.html#float) **Default:** `read_timeout` ##### process\_tool\_call Hook to customize tool calling and optionally pass extra metadata. **Type:** `ProcessToolCallback` | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `process_tool_call` ##### allow\_sampling Whether to allow MCP sampling through this client. **Type:** [`bool`](https://docs.python.org/3/library/functions.html#bool) **Default:** `allow_sampling` ##### sampling\_model The model to use for sampling. **Type:** [`models.Model`](/docs/ai/api/models/base/#pydantic_ai.models.Model) | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `sampling_model` ##### max\_retries The maximum number of times to retry a tool call. **Type:** [`int`](https://docs.python.org/3/library/functions.html#int) **Default:** `max_retries` ##### elicitation\_callback Callback function to handle elicitation requests from the server. **Type:** `ElicitationFnT` | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `elicitation_callback` ##### cache\_tools Whether to cache the list of tools. When enabled (default), tools are fetched once and cached until either: - The server sends a `notifications/tools/list_changed` notification - `MCPServer.__aexit__` is called (when the last context exits) Set to `False` for servers that change tools dynamically without sending notifications. Note: When using durable execution (Temporal, DBOS), tool definitions are additionally cached at the wrapper level across activities/steps, to avoid redundant MCP connections. This wrapper-level cache is not invalidated by `tools/list_changed` notifications. Set to `False` to disable all caching if tools may change during a workflow. **Type:** [`bool`](https://docs.python.org/3/library/functions.html#bool) **Default:** `cache_tools` ##### cache\_resources Whether to cache the list of resources. When enabled (default), resources are fetched once and cached until either: - The server sends a `notifications/resources/list_changed` notification - `MCPServer.__aexit__` is called (when the last context exits) Set to `False` for servers that change resources dynamically without sending notifications. **Type:** [`bool`](https://docs.python.org/3/library/functions.html#bool) **Default:** `cache_resources` ##### include\_instructions Whether to include the server's instructions in the agent's instructions. Defaults to `False` for backward compatibility. **Type:** [`bool`](https://docs.python.org/3/library/functions.html#bool) **Default:** `include_instructions` ##### include\_return\_schema Whether to include return schemas in tool definitions sent to the model. When `None` (default), defaults to `False` unless the [`IncludeToolReturnSchemas`](/docs/ai/api/pydantic-ai/capabilities/#pydantic_ai.capabilities.IncludeToolReturnSchemas) capability is used. **Type:** [`bool`](https://docs.python.org/3/library/functions.html#bool) | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `include_return_schema` ##### server\_info Access the information send by the MCP server during initialization. **Type:** `mcp_types.Implementation` ##### capabilities Access the capabilities advertised by the MCP server during initialization. **Type:** `ServerCapabilities` ##### instructions Access the instructions sent by the MCP server during initialization. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) ##### is\_running Check if the MCP server is running. **Type:** [`bool`](https://docs.python.org/3/library/functions.html#bool) #### Methods ##### client\_streams `@abstractmethod` `@async` ```python def client_streams( ) -> AsyncIterator[tuple[MemoryObjectReceiveStream[SessionMessage | Exception], MemoryObjectSendStream[SessionMessage]]] ``` Create the streams for the MCP server. ###### Returns [`AsyncIterator`](https://docs.python.org/3/library/typing.html#typing.AsyncIterator)\[[`tuple`](https://docs.python.org/3/library/stdtypes.html#tuple)\[`MemoryObjectReceiveStream`\[`SessionMessage` | [`Exception`](https://docs.python.org/3/library/exceptions.html#Exception)\], `MemoryObjectSendStream`\[`SessionMessage`\]\]\] ##### get\_instructions `@async` ```python def get_instructions(ctx: RunContext[Any]) -> messages.InstructionPart | None ``` Return the MCP server's instructions for how to use its tools. If [`include_instructions`](/docs/ai/api/pydantic-ai/mcp/#pydantic_ai.mcp.MCPServer.include_instructions) is `True`, returns the [`instructions`](/docs/ai/api/pydantic-ai/mcp/#pydantic_ai.mcp.MCPServer.instructions) sent by the MCP server during initialization. Otherwise, returns `None`. Instructions from external servers are marked as dynamic since they may change between connections. ###### Returns [`messages.InstructionPart`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.InstructionPart) | [`None`](https://docs.python.org/3/library/constants.html#None) -- An `InstructionPart` with the server's instructions if `include_instructions` is enabled, otherwise `None`. ###### Parameters **`ctx`** : [`RunContext`](/docs/ai/api/pydantic-ai/tools/#pydantic_ai.tools.RunContext)\[[`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] The run context for this agent run. ##### list\_tools `@async` ```python def list_tools() -> list[mcp_types.Tool] ``` Retrieve tools that are currently active on the server. Tools are cached by default, with cache invalidation on: - `notifications/tools/list_changed` notifications from the server - `__aexit__` when the last context exits Set `cache_tools=False` for servers that change tools without sending notifications. ###### Returns [`list`](https://docs.python.org/3/glossary.html#term-list)\[`mcp_types.Tool`\] ##### direct\_call\_tool `@async` ```python def direct_call_tool( name: str, args: dict[str, Any], metadata: dict[str, Any] | None = None, ) -> ToolResult ``` Call a tool on the server. ###### Returns `ToolResult` -- The result of the tool call. ###### Parameters **`name`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) The name of the tool to call. **`args`** : [`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] The arguments to pass to the tool. **`metadata`** : [`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Request-level metadata (optional) ###### Raises - `ModelRetry` -- If the tool call fails. ##### list\_resources `@async` ```python def list_resources() -> list[Resource] ``` Retrieve resources that are currently present on the server. Resources are cached by default, with cache invalidation on: - `notifications/resources/list_changed` notifications from the server - `__aexit__` when the last context exits Set `cache_resources=False` for servers that change resources without sending notifications. ###### Returns [`list`](https://docs.python.org/3/glossary.html#term-list)\[`Resource`\] ###### Raises - `MCPError` -- If the server returns an error. ##### list\_resource\_templates `@async` ```python def list_resource_templates() -> list[ResourceTemplate] ``` Retrieve resource templates that are currently present on the server. ###### Returns [`list`](https://docs.python.org/3/glossary.html#term-list)\[`ResourceTemplate`\] ###### Raises - `MCPError` -- If the server returns an error. ##### read\_resource `@async` ```python def read_resource( uri: str, ) -> str | messages.BinaryContent | list[str | messages.BinaryContent] def read_resource( uri: Resource, ) -> str | messages.BinaryContent | list[str | messages.BinaryContent] ``` Read the contents of a specific resource by URI. ###### Returns [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`messages.BinaryContent`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.BinaryContent) | [`list`](https://docs.python.org/3/glossary.html#term-list)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`messages.BinaryContent`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.BinaryContent)\] -- The resource contents. If the resource has a single content item, returns that item directly. [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`messages.BinaryContent`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.BinaryContent) | [`list`](https://docs.python.org/3/glossary.html#term-list)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`messages.BinaryContent`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.BinaryContent)\] -- If the resource has multiple content items, returns a list of items. ###### Parameters **`uri`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | `Resource` The URI of the resource to read, or a Resource object. ###### Raises - `MCPError` -- If the server returns an error. ##### \_\_aenter\_\_ `@async` ```python def __aenter__() -> Self ``` Enter the MCP server context. The first call starts the connection (spawning a subprocess for stdio servers, opening an HTTP connection for HTTP servers). Subsequent calls -- from any task -- share the same connection via reference counting. The connection is torn down when the last `async with` scope exits. Because the session runs in a dedicated background task, entering and exiting from different tasks (e.g. `asyncio.gather` children, fasta2a workers, or graph node tasks) is safe: the underlying transport's cancel scopes never cross task boundaries. ###### Returns [`Self`](https://docs.python.org/3/library/typing.html#typing.Self) ### MCPServerStdio **Bases:** `MCPServer` Runs an MCP server in a subprocess and communicates with it over stdin/stdout. This class implements the stdio transport from the MCP specification. See [https://spec.modelcontextprotocol.io/specification/2024-11-05/basic/transports/#stdio](https://spec.modelcontextprotocol.io/specification/2024-11-05/basic/transports/#stdio) for more information. Note Using this class as an async context manager will start the server as a subprocess when entering the context, and stop it when exiting the context. Example: ```python from pydantic_ai import Agent from pydantic_ai.mcp import MCPServerStdio server = MCPServerStdio( # (1) 'uv', args=['run', 'mcp-run-python', 'stdio'], timeout=10 ) agent = Agent('openai:gpt-5.2', toolsets=[server]) ``` See [MCP Run Python](https://github.com/pydantic/mcp-run-python) for more information. #### Attributes ##### command The command to run. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) **Default:** `command` ##### args The arguments to pass to the command. **Type:** [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str)\] **Default:** `args` ##### env The environment variables the CLI server will have access to. By default the subprocess will not inherit any environment variables from the parent process. If you want to inherit the environment variables from the parent process, use `env=os.environ`. **Type:** [`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), [`str`](https://docs.python.org/3/library/stdtypes.html#str)\] | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `env` ##### cwd The working directory to use when spawning the process. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) | `Path` | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `cwd` #### Methods ##### \_\_init\_\_ ```python def __init__( command: str, args: Sequence[str], env: dict[str, str] | None = None, cwd: str | Path | None = None, tool_prefix: str | None = None, log_level: mcp_types.LoggingLevel | None = None, log_handler: LoggingFnT | None = None, timeout: float = 5, read_timeout: float = 5 * 60, process_tool_call: ProcessToolCallback | None = None, allow_sampling: bool = True, sampling_model: models.Model | None = None, max_retries: int = 1, elicitation_callback: ElicitationFnT | None = None, cache_tools: bool = True, cache_resources: bool = True, include_instructions: bool = False, include_return_schema: bool | None = None, id: str | None = None, client_info: mcp_types.Implementation | None = None, ) ``` Build a new MCP server. ###### Parameters **`command`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) The command to run. **`args`** : [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str)\] The arguments to pass to the command. **`env`** : [`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), [`str`](https://docs.python.org/3/library/stdtypes.html#str)\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` The environment variables to set in the subprocess. **`cwd`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | `Path` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` The working directory to use when spawning the process. **`tool_prefix`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` A prefix to add to all tools that are registered with the server. **`log_level`** : `mcp_types.LoggingLevel` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` The log level to set when connecting to the server, if any. **`log_handler`** : `LoggingFnT` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` A handler for logging messages from the server. **`timeout`** : [`float`](https://docs.python.org/3/library/functions.html#float) _Default:_ `5` The timeout in seconds to wait for the client to initialize. **`read_timeout`** : [`float`](https://docs.python.org/3/library/functions.html#float) _Default:_ `5 * 60` Maximum time in seconds to wait for new messages before timing out. **`process_tool_call`** : `ProcessToolCallback` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Hook to customize tool calling and optionally pass extra metadata. **`allow_sampling`** : [`bool`](https://docs.python.org/3/library/functions.html#bool) _Default:_ `True` Whether to allow MCP sampling through this client. **`sampling_model`** : [`models.Model`](/docs/ai/api/models/base/#pydantic_ai.models.Model) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` The model to use for sampling. **`max_retries`** : [`int`](https://docs.python.org/3/library/functions.html#int) _Default:_ `1` The maximum number of times to retry a tool call. **`elicitation_callback`** : `ElicitationFnT` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Callback function to handle elicitation requests from the server. **`cache_tools`** : [`bool`](https://docs.python.org/3/library/functions.html#bool) _Default:_ `True` Whether to cache the list of tools. See [`MCPServer.cache_tools`](/docs/ai/api/pydantic-ai/mcp/#pydantic_ai.mcp.MCPServer.cache_tools). **`cache_resources`** : [`bool`](https://docs.python.org/3/library/functions.html#bool) _Default:_ `True` Whether to cache the list of resources. See [`MCPServer.cache_resources`](/docs/ai/api/pydantic-ai/mcp/#pydantic_ai.mcp.MCPServer.cache_resources). **`include_instructions`** : [`bool`](https://docs.python.org/3/library/functions.html#bool) _Default:_ `False` Whether to include the server's instructions in the agent's instructions. See [`MCPServer.include_instructions`](/docs/ai/api/pydantic-ai/mcp/#pydantic_ai.mcp.MCPServer.include_instructions). **`include_return_schema`** : [`bool`](https://docs.python.org/3/library/functions.html#bool) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Whether to include return schemas in tool definitions. See [`MCPServer.include_return_schema`](/docs/ai/api/pydantic-ai/mcp/#pydantic_ai.mcp.MCPServer.include_return_schema). **`id`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` An optional unique ID for the MCP server. An MCP server needs to have an ID in order to be used in a durable execution environment like Temporal, in which case the ID will be used to identify the server's activities within the workflow. **`client_info`** : `mcp_types.Implementation` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Information describing the MCP client implementation. ### MCPServerSSE **Bases:** `_MCPServerHTTP` An MCP server that connects over streamable HTTP connections. This class implements the SSE transport from the MCP specification. See [https://spec.modelcontextprotocol.io/specification/2024-11-05/basic/transports/#http-with-sse](https://spec.modelcontextprotocol.io/specification/2024-11-05/basic/transports/#http-with-sse) for more information. Note Using this class as an async context manager will create a new pool of HTTP connections to connect to a server which should already be running. Example: ```python from pydantic_ai import Agent from pydantic_ai.mcp import MCPServerSSE server = MCPServerSSE('http://localhost:3001/sse') agent = Agent('openai:gpt-5.2', toolsets=[server]) ``` ### MCPServerHTTP **Bases:** `MCPServerSSE` An MCP server that connects over HTTP using the old SSE transport. This class implements the SSE transport from the MCP specification. See [https://spec.modelcontextprotocol.io/specification/2024-11-05/basic/transports/#http-with-sse](https://spec.modelcontextprotocol.io/specification/2024-11-05/basic/transports/#http-with-sse) for more information. Note Using this class as an async context manager will create a new pool of HTTP connections to connect to a server which should already be running. Example: ```python from pydantic_ai import Agent from pydantic_ai.mcp import MCPServerHTTP server = MCPServerHTTP('http://localhost:3001/sse') agent = Agent('openai:gpt-5.2', toolsets=[server]) ``` ### MCPServerStreamableHTTP **Bases:** `_MCPServerHTTP` An MCP server that connects over HTTP using the Streamable HTTP transport. This class implements the Streamable HTTP transport from the MCP specification. See [https://modelcontextprotocol.io/introduction#streamable-http](https://modelcontextprotocol.io/introduction#streamable-http) for more information. Note Using this class as an async context manager will create a new pool of HTTP connections to connect to a server which should already be running. Example: ```python from pydantic_ai import Agent from pydantic_ai.mcp import MCPServerStreamableHTTP server = MCPServerStreamableHTTP('http://localhost:8000/mcp') agent = Agent('openai:gpt-5.2', toolsets=[server]) ``` ### CallToolFunc **Bases:** [`Protocol`](https://docs.python.org/3/library/typing.html#typing.Protocol) A callable that invokes an MCP tool -- typically `MCPToolset.direct_call_tool` or its legacy equivalent. Passed to user-defined [`ProcessToolCallback`](/docs/ai/api/pydantic-ai/mcp/#pydantic_ai.mcp.ProcessToolCallback) functions as the underlying call hook. `metadata` is keyword-only -- pass it as `await call_tool(name, args, metadata=...)`. ### MCPToolset **Bases:** `AbstractToolset[AgentDepsT]` A toolset for connecting to an MCP server. `MCPToolset` is the recommended way to use [Model Context Protocol](https://modelcontextprotocol.io) servers in Pydantic AI. It is built on the [FastMCP](https://gofastmcp.com) `Client`, which supports the full MCP protocol -- tools, resources, sampling, elicitation, OAuth -- and a wide range of transports (HTTP, SSE, stdio, in-process FastMCP servers, multi-server configs). Pass any input that FastMCP can build a transport from -- a URL, a script path, a `FastMCP` server instance for in-process testing -- or a pre-built `fastmcp.Client` for full control over its configuration. For multi-server JSON config files, use [`load_mcp_toolsets`](/docs/ai/api/pydantic-ai/mcp/#pydantic_ai.mcp.load_mcp_toolsets) instead. Example -- connect to a streamable-HTTP MCP server: ```python from pydantic_ai import Agent from pydantic_ai.mcp import MCPToolset toolset = MCPToolset('http://localhost:8000/mcp') agent = Agent('openai:gpt-5', toolsets=[toolset]) ``` Example -- connect to a local stdio MCP server: ```python from pydantic_ai.mcp import MCPToolset toolset = MCPToolset('my_mcp_server.py') ``` Example -- pass a pre-built FastMCP Client for full configuration control: ```python from fastmcp.client import Client from fastmcp.client.transports import StreamableHttpTransport from pydantic_ai.mcp import MCPToolset client = Client(StreamableHttpTransport('http://localhost:8000/mcp'), auth='oauth') toolset = MCPToolset(client) ``` #### Attributes ##### client The underlying FastMCP `Client`. Always normalized to a `fastmcp.Client` regardless of how the toolset was constructed. **Type:** `FastMCPClient`\[[`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] ##### max\_retries Maximum number of times a tool call may be retried after a `ModelRetry`. `None` (default) inherits the agent's retry count at runtime. Set explicitly to override. **Type:** [`int`](https://docs.python.org/3/library/functions.html#int) | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `max_retries` ##### tool\_error\_behavior How to handle tool errors raised by the server. `'retry'` (default) raises [`ModelRetry`](/docs/ai/api/pydantic-ai/exceptions/#pydantic_ai.exceptions.ModelRetry) so the model can self-correct; `'error'` propagates the underlying `fastmcp.exceptions.ToolError` to the caller. **Type:** [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['retry', 'error'\] **Default:** `tool_error_behavior` ##### process\_tool\_call Hook to wrap tool calls -- useful for adding request-level metadata, custom retry policies, or telemetry. See [`ProcessToolCallback`](/docs/ai/api/pydantic-ai/mcp/#pydantic_ai.mcp.ProcessToolCallback). **Type:** `ProcessToolCallback` | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `process_tool_call` ##### cache\_tools Whether to cache the list of tools across `get_tools()` calls. When enabled (default), tools are fetched once and cached until either: - The server sends a `notifications/tools/list_changed` notification - The toolset is fully exited (last `__aexit__` matches the first `__aenter__`) Set to `False` for servers that change tools dynamically without sending notifications, or when passing a pre-built FastMCP Client (the cache-invalidation message handler isn't installed in that case, so caches are only invalidated by session close). **Type:** [`bool`](https://docs.python.org/3/library/functions.html#bool) **Default:** `cache_tools` ##### cache\_resources Whether to cache the list of resources across `list_resources()` calls. Same semantics as [`cache_tools`](/docs/ai/api/pydantic-ai/mcp/#pydantic_ai.mcp.MCPToolset.cache_tools) but for `notifications/resources/list_changed` notifications. **Type:** [`bool`](https://docs.python.org/3/library/functions.html#bool) **Default:** `cache_resources` ##### include\_instructions Whether to include the server's `initialize` instructions string in the agent's instruction set. Defaults to `False` for backward compatibility. When `True`, the instructions returned by the server during initialization are added to the agent's instructions. **Type:** [`bool`](https://docs.python.org/3/library/functions.html#bool) **Default:** `include_instructions` ##### include\_return\_schema Whether to include each tool's `outputSchema` in the schema sent to the model. When `None` (the default), defaults to `False` unless the [`IncludeToolReturnSchemas`](/docs/ai/api/pydantic-ai/capabilities/#pydantic_ai.capabilities.IncludeToolReturnSchemas) capability is used. **Type:** [`bool`](https://docs.python.org/3/library/functions.html#bool) | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `include_return_schema` ##### sampling\_model A Pydantic AI model that the server may sample from via the MCP `sampling/createMessage` flow. When set (and no explicit `sampling_handler` is passed), Pydantic AI builds a sampling handler that delegates to this model with the request's `maxTokens`/`temperature`/`stopSequences` settings applied. If both `sampling_model` and `sampling_handler` are passed, an error is raised. **Type:** [`models.Model`](/docs/ai/api/models/base/#pydantic_ai.models.Model) | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `sampling_model` ##### log\_level Log level requested from the server via `logging/setLevel` after initialization. `None` (default) leaves the server's default log level alone. Combine with `log_handler` to receive log messages. **Type:** `mcp_types.LoggingLevel` | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `log_level` ##### server\_info The server-implementation info sent during initialization. Raises [`AttributeError`](https://docs.python.org/3/library/exceptions.html#AttributeError) when accessed before the toolset has been entered. **Type:** `mcp_types.Implementation` ##### capabilities The capabilities advertised by the server during initialization. Raises [`AttributeError`](https://docs.python.org/3/library/exceptions.html#AttributeError) when accessed before the toolset has been entered. **Type:** `ServerCapabilities` ##### instructions The instructions sent by the server during initialization. Raises [`AttributeError`](https://docs.python.org/3/library/exceptions.html#AttributeError) when accessed before the toolset has been entered. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) ##### is\_running Whether the toolset is currently entered (the FastMCP session is open). **Type:** [`bool`](https://docs.python.org/3/library/functions.html#bool) #### Methods ##### \_\_init\_\_ ```python def __init__( client: MCPToolsetClient, id: str | None = None, max_retries: int | None = None, tool_error_behavior: Literal['retry', 'error'] = 'retry', process_tool_call: ProcessToolCallback | None = None, cache_tools: bool = True, cache_resources: bool = True, include_instructions: bool = False, include_return_schema: bool | None = None, sampling_model: models.Model | None = None, sampling_handler: SamplingHandler[Any, Any] | None = None, elicitation_handler: ElicitationHandler[Any, Any] | None = None, log_handler: LogHandler | None = None, log_level: mcp_types.LoggingLevel | None = None, progress_handler: ProgressHandler | None = None, message_handler: MessageHandlerT | None = None, client_info: mcp_types.Implementation | None = None, init_timeout: float | None = _UNSET, read_timeout: float | None = _UNSET, roots: RootsList | RootsHandler[Any] | None = None, auth: httpx.Auth | Literal['oauth'] | str | None = None, verify: ssl.SSLContext | bool | str | None = None, headers: dict[str, str] | None = None, http_client: httpx.AsyncClient | None = None, ) ``` Build a new `MCPToolset`. ###### Parameters **`client`** : `MCPToolsetClient` How to connect to the MCP server. See the class docstring for accepted shapes. **`id`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` An optional unique identifier for this toolset. Required for use in durable execution environments like Temporal or DBOS, where it identifies the toolset's activities/steps within a workflow. **`max_retries`** : [`int`](https://docs.python.org/3/library/functions.html#int) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Maximum number of times a tool call may be retried after a `ModelRetry`. `None` inherits the agent's retry count at runtime. **`tool_error_behavior`** : [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['retry', 'error'\] _Default:_ `'retry'` `'retry'` (default) raises [`ModelRetry`](/docs/ai/api/pydantic-ai/exceptions/#pydantic_ai.exceptions.ModelRetry) on tool errors so the model can self-correct; `'error'` propagates the underlying exception. **`process_tool_call`** : `ProcessToolCallback` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Hook to wrap tool calls. See [`ProcessToolCallback`](/docs/ai/api/pydantic-ai/mcp/#pydantic_ai.mcp.ProcessToolCallback). **`cache_tools`** : [`bool`](https://docs.python.org/3/library/functions.html#bool) _Default:_ `True` Whether to cache the list of tools. See [`MCPToolset.cache_tools`](/docs/ai/api/pydantic-ai/mcp/#pydantic_ai.mcp.MCPToolset.cache_tools). **`cache_resources`** : [`bool`](https://docs.python.org/3/library/functions.html#bool) _Default:_ `True` Whether to cache the list of resources. See [`MCPToolset.cache_resources`](/docs/ai/api/pydantic-ai/mcp/#pydantic_ai.mcp.MCPToolset.cache_resources). **`include_instructions`** : [`bool`](https://docs.python.org/3/library/functions.html#bool) _Default:_ `False` Whether to include the server's instructions in the agent's instructions. See [`MCPToolset.include_instructions`](/docs/ai/api/pydantic-ai/mcp/#pydantic_ai.mcp.MCPToolset.include_instructions). **`include_return_schema`** : [`bool`](https://docs.python.org/3/library/functions.html#bool) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Whether to include return schemas in tool definitions. See [`MCPToolset.include_return_schema`](/docs/ai/api/pydantic-ai/mcp/#pydantic_ai.mcp.MCPToolset.include_return_schema). **`sampling_model`** : [`models.Model`](/docs/ai/api/models/base/#pydantic_ai.models.Model) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` A Pydantic AI model the server may sample from. Mutually exclusive with `sampling_handler`. **`sampling_handler`** : `SamplingHandler`\[[`Any`](https://docs.python.org/3/library/typing.html#typing.Any), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` A FastMCP-shaped sampling handler. Use for full control over the sampling response. **`elicitation_handler`** : `ElicitationHandler`\[[`Any`](https://docs.python.org/3/library/typing.html#typing.Any), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` A FastMCP-shaped elicitation handler that receives MCP `elicitation/create` requests from the server. **`log_handler`** : `LogHandler` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` A FastMCP-shaped log handler that receives log messages from the server. **`log_level`** : `mcp_types.LoggingLevel` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Log level requested from the server via `logging/setLevel` after initialization. **`progress_handler`** : `ProgressHandler` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` A FastMCP-shaped progress handler. **`message_handler`** : `MessageHandlerT` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` A FastMCP-shaped message handler called for every server-sent message. Pydantic AI installs its own message handler internally to invalidate caches on `list_changed` notifications; if you provide one, both run (yours after ours). **`client_info`** : `mcp_types.Implementation` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Information describing the MCP client implementation, sent to the server during initialization. **`init_timeout`** : [`float`](https://docs.python.org/3/library/functions.html#float) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `_UNSET` Timeout in seconds for the initial connection and `initialize` handshake. **`read_timeout`** : [`float`](https://docs.python.org/3/library/functions.html#float) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `_UNSET` Maximum time in seconds to wait for new messages on the long-lived connection. Defaults to 5 minutes. **`roots`** : `RootsList` | `RootsHandler`\[[`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Filesystem roots advertised to the server. **`auth`** : `httpx.Auth` | [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['oauth'\] | [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` HTTP authentication for HTTP transports -- an `httpx.Auth`, the literal string `'oauth'` to enable FastMCP's OAuth flow, or a bearer-token string. **`verify`** : [`ssl.SSLContext`](https://docs.python.org/3/library/ssl.html#ssl.SSLContext) | [`bool`](https://docs.python.org/3/library/functions.html#bool) | [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` SSL verification mode for HTTP transports -- an `ssl.SSLContext`, a CA bundle path string, or a bool. **`headers`** : [`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), [`str`](https://docs.python.org/3/library/stdtypes.html#str)\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Extra HTTP headers for HTTP transports. Mutually exclusive with `http_client`. **`http_client`** : `httpx.AsyncClient` | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` A pre-configured `httpx.AsyncClient` to use for HTTP transports -- useful for self-signed certificates or custom connection pooling. Mutually exclusive with `headers`. ###### Raises - `ValueError` -- If a pre-built `fastmcp.Client` is passed alongside any of the kwargs that would otherwise build a default Client (sampling, elicitation, headers, etc.), or if `sampling_model` and `sampling_handler` are both passed, or if `headers` and `http_client` are both passed. - `ImportError` -- If the fastmcp client isn't installed. Install the `mcp` extra (which pulls `fastmcp-slim[client]`): `pip install "pydantic-ai-slim[mcp]"`. ##### get\_instructions `@async` ```python def get_instructions(ctx: RunContext[AgentDepsT]) -> messages.InstructionPart | None ``` Return the server's instructions if `include_instructions` is enabled. ###### Returns [`messages.InstructionPart`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.InstructionPart) | [`None`](https://docs.python.org/3/library/constants.html#None) ##### list\_tools `@async` ```python def list_tools() -> list[mcp_types.Tool] ``` Retrieve the tools currently exposed by the server. When [`cache_tools`](/docs/ai/api/pydantic-ai/mcp/#pydantic_ai.mcp.MCPToolset.cache_tools) is enabled (default), results are cached and invalidated by `notifications/tools/list_changed` or the toolset's last `__aexit__`. ###### Returns [`list`](https://docs.python.org/3/glossary.html#term-list)\[`mcp_types.Tool`\] ##### direct\_call\_tool `@async` ```python def direct_call_tool( name: str, args: dict[str, Any], metadata: dict[str, Any] | None = None, ) -> Any ``` Call a tool on the server directly. ###### Returns [`Any`](https://docs.python.org/3/library/typing.html#typing.Any) ###### Parameters **`name`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) The name of the tool to call. **`args`** : [`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] The arguments to pass to the tool. **`metadata`** : [`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] | [`None`](https://docs.python.org/3/library/constants.html#None) _Default:_ `None` Optional request-level `_meta` payload sent alongside the call. ###### Raises - `ModelRetry` -- If the tool errors and `tool_error_behavior='retry'` (the default). - `fastmcp.exceptions.ToolError` -- If the tool errors and `tool_error_behavior='error'`. ##### list\_resources `@async` ```python def list_resources() -> list[Resource] ``` Retrieve the resources currently exposed by the server. When [`cache_resources`](/docs/ai/api/pydantic-ai/mcp/#pydantic_ai.mcp.MCPToolset.cache_resources) is enabled (default), results are cached and invalidated by `notifications/resources/list_changed` or the toolset's last `__aexit__`. Returns an empty list if the server does not advertise the `resources` capability. ###### Returns [`list`](https://docs.python.org/3/glossary.html#term-list)\[`Resource`\] ###### Raises - `MCPError` -- If the server returns an error. ##### list\_resource\_templates `@async` ```python def list_resource_templates() -> list[ResourceTemplate] ``` Retrieve the resource templates currently exposed by the server. Returns an empty list if the server does not advertise the `resources` capability. ###### Returns [`list`](https://docs.python.org/3/glossary.html#term-list)\[`ResourceTemplate`\] ###### Raises - `MCPError` -- If the server returns an error. ##### read\_resource `@async` ```python def read_resource( uri: str, ) -> str | messages.BinaryContent | list[str | messages.BinaryContent] def read_resource( uri: Resource, ) -> str | messages.BinaryContent | list[str | messages.BinaryContent] ``` Read the contents of a specific resource by URI. ###### Returns [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`messages.BinaryContent`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.BinaryContent) | [`list`](https://docs.python.org/3/glossary.html#term-list)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`messages.BinaryContent`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.BinaryContent)\] -- The resource contents -- a single value if the resource has one content item, or a list [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`messages.BinaryContent`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.BinaryContent) | [`list`](https://docs.python.org/3/glossary.html#term-list)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`messages.BinaryContent`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.BinaryContent)\] -- otherwise. Text content is returned as `str`, binary content as [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`messages.BinaryContent`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.BinaryContent) | [`list`](https://docs.python.org/3/glossary.html#term-list)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`messages.BinaryContent`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.BinaryContent)\] -- [`BinaryContent`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.BinaryContent). ###### Parameters **`uri`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | `Resource` The URI of the resource to read, or a [`Resource`](/docs/ai/api/pydantic-ai/mcp/#pydantic_ai.mcp.Resource) object. ###### Raises - `MCPError` -- If the server returns an error. ### load\_mcp\_servers `@deprecated` ```python def load_mcp_servers( config_path: str | Path, ) -> list[MCPServerStdio | MCPServerStreamableHTTP | MCPServerSSE] ``` Load MCP servers from a configuration file. Environment variables can be referenced in the configuration file using: - `${VAR_NAME}` syntax - expands to the value of VAR\_NAME, raises error if not defined - `${VAR_NAME:-default}` syntax - expands to VAR\_NAME if set, otherwise uses the default value #### Returns [`list`](https://docs.python.org/3/glossary.html#term-list)\[`MCPServerStdio` | `MCPServerStreamableHTTP` | `MCPServerSSE`\] -- A list of MCP servers. #### Parameters **`config_path`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | `Path` The path to the configuration file. #### Raises - `FileNotFoundError` -- If the configuration file does not exist. - `ValidationError` -- If the configuration file does not match the schema. - `ValueError` -- If an environment variable referenced in the configuration is not defined and no default value is provided. ### load\_mcp\_toolsets ```python def load_mcp_toolsets(config_path: str | Path) -> list[AbstractToolset[Any]] ``` Load `MCPToolset`s from a configuration file. The configuration file uses the same `mcpServers` JSON shape as Claude Desktop, Cursor, and the MCP specification. Each server entry produces one [`MCPToolset`](/docs/ai/api/pydantic-ai/mcp/#pydantic_ai.mcp.MCPToolset), wrapped in a [`PrefixedToolset`](/docs/ai/api/pydantic-ai/toolsets/#pydantic_ai.toolsets.PrefixedToolset) using the server's name as prefix to disambiguate tools across multiple servers. Environment variables can be referenced in the configuration file using: - `${VAR_NAME}` syntax -- expands to the value of `VAR_NAME`, raises if not defined - `${VAR_NAME:-default}` syntax -- expands to `VAR_NAME` if set, otherwise the default #### Returns [`list`](https://docs.python.org/3/glossary.html#term-list)\[[`AbstractToolset`](/docs/ai/api/pydantic-ai/toolsets/#pydantic_ai.toolsets.AbstractToolset)\[[`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\]\] -- A list of toolsets, one per server in the config file, each prefixed with the server name. #### Parameters **`config_path`** : [`str`](https://docs.python.org/3/library/stdtypes.html#str) | `Path` Path to the JSON configuration file. #### Raises - `FileNotFoundError` -- If the configuration file does not exist. - `ValidationError` -- If the configuration file does not match the schema. - `ValueError` -- If an environment variable referenced in the configuration is not defined and no default is provided. - `ImportError` -- If the fastmcp client isn't installed. Install the `mcp` extra (which pulls `fastmcp-slim[client]`): `pip install "pydantic-ai-slim[mcp]"`. ### ToolResult The result type of an MCP tool call. **Default:** `str | messages.BinaryContent | dict[str, Any] | list[Any] | Sequence[str | messages.BinaryContent | dict[str, Any] | list[Any]]` ### ProcessToolCallback A process tool callback. It accepts a run context, the original tool call function, a tool name, and arguments. Allows wrapping an MCP server tool call to customize it, including adding extra request metadata. **Default:** `Callable[[RunContext[Any], CallToolFunc, str, dict[str, Any]], Awaitable[ToolResult]]` ### MCPToolsetClient Anything `MCPToolset` accepts as its `client` argument -- a pre-built `fastmcp.Client`, a FastMCP `ClientTransport`, an in-process `FastMCP` server, an `AnyUrl`/URL string, a script `Path`, or a URL/path/script string. For multi-server JSON config files, use [`load_mcp_toolsets`](/docs/ai/api/pydantic-ai/mcp/#pydantic_ai.mcp.load_mcp_toolsets) instead -- it expands env vars and constructs one `MCPToolset` per server entry. **Type:** [`TypeAlias`](https://docs.python.org/3/library/typing.html#typing.TypeAlias) **Default:** `'FastMCPClient[Any] | ClientTransport | FastMCP | FastMCP1Server | AnyUrl | Path | str'` --- # [pydantic_ai.messages](https://pydantic.dev/docs/ai/api/pydantic-ai/messages/) # pydantic\_ai.messages The structure of [`ModelMessage`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.ModelMessage) can be shown as a graph: ```mermaid graph RL SystemPromptPart(SystemPromptPart) --- ModelRequestPart UserPromptPart(UserPromptPart) --- ModelRequestPart ToolReturnPart(ToolReturnPart) --- ModelRequestPart RetryPromptPart(RetryPromptPart) --- ModelRequestPart TextPart(TextPart) --- ModelResponsePart ToolCallPart(ToolCallPart) --- ModelResponsePart ThinkingPart(ThinkingPart) --- ModelResponsePart ModelRequestPart("ModelRequestPart
(Union)") --- ModelRequest ModelRequest("ModelRequest(parts=list[...])") --- ModelMessage ModelResponsePart("ModelResponsePart
(Union)") --- ModelResponse ModelResponse("ModelResponse(parts=list[...])") --- ModelMessage("ModelMessage
(Union)") ``` ### SystemPromptPart A system prompt, generally written by the application developer. This gives the model context and guidance on how to respond. #### Attributes ##### content The content of the prompt. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### timestamp The timestamp of the prompt. **Type:** [`datetime`](https://docs.python.org/3/library/datetime.html#module-datetime) **Default:** `field(default_factory=_now_utc)` ##### dynamic\_ref The ref of the dynamic system prompt function that generated this part. Only set if system prompt is dynamic, see [`system_prompt`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.Agent.system_prompt) for more information. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `None` ##### part\_kind Part type identifier, this is available on all parts as a discriminator. **Type:** [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['system-prompt'\] **Default:** `'system-prompt'` ### FileUrl **Bases:** `ABC` Abstract base class for any URL-based file. #### Attributes ##### url The URL of the file. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### force\_download Controls whether the file is downloaded and how SSRF protection is applied: - If `False`, the URL is sent directly to providers that support it. For providers that don't, the file is downloaded with SSRF protection (blocks private IPs and cloud metadata). - If `True`, the file is always downloaded with SSRF protection (blocks private IPs and cloud metadata). - If `'allow-local'`, the file is always downloaded, allowing private IPs but still blocking cloud metadata. **Type:** `ForceDownloadMode` **Default:** `False` ##### vendor\_metadata Vendor-specific metadata for the file. Supported by: - `GoogleModel`: `VideoUrl.vendor_metadata` is used as `video_metadata`: [https://ai.google.dev/gemini-api/docs/video-understanding#customize-video-processing](https://ai.google.dev/gemini-api/docs/video-understanding#customize-video-processing) - `OpenAIChatModel`, `OpenAIResponsesModel`: `ImageUrl.vendor_metadata['detail']` is used as `detail` setting for images - `XaiModel`: `ImageUrl.vendor_metadata['detail']` is used as `detail` setting for images **Type:** [`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `None` ##### media\_type Return the media type of the file, based on the URL or the provided `media_type`. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### identifier The identifier of the file, such as a unique ID. This identifier can be provided to the model in a message to allow it to refer to this file in a tool call argument, and the tool can look up the file in question by iterating over the message history and finding the matching `FileUrl`. This identifier is only automatically passed to the model when the `FileUrl` is returned by a tool. If you're passing the `FileUrl` as a user message, it's up to you to include a separate text part with the identifier, e.g. "This is file :" preceding the `FileUrl`. It's also included in inline-text delimiters for providers that require inlining text documents, so the model can distinguish multiple files. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### format The file format. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ### VideoUrl **Bases:** [`FileUrl`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.FileUrl) A URL to a video. #### Attributes ##### url The URL of the video. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### kind Type identifier, this is available on all parts as a discriminator. **Type:** [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['video-url'\] **Default:** `'video-url'` ##### is\_youtube True if the URL has a YouTube domain. **Type:** [`bool`](https://docs.python.org/3/library/functions.html#bool) ##### format The file format of the video. The choice of supported formats were based on the Bedrock Converse API. Other APIs don't require to use a format. **Type:** `VideoFormat` ### AudioUrl **Bases:** [`FileUrl`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.FileUrl) A URL to an audio file. #### Attributes ##### url The URL of the audio file. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### kind Type identifier, this is available on all parts as a discriminator. **Type:** [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['audio-url'\] **Default:** `'audio-url'` ##### format The file format of the audio file. **Type:** `AudioFormat` ### ImageUrl **Bases:** [`FileUrl`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.FileUrl) A URL to an image. #### Attributes ##### url The URL of the image. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### kind Type identifier, this is available on all parts as a discriminator. **Type:** [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['image-url'\] **Default:** `'image-url'` ##### format The file format of the image. The choice of supported formats were based on the Bedrock Converse API. Other APIs don't require to use a format. **Type:** `ImageFormat` ### DocumentUrl **Bases:** [`FileUrl`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.FileUrl) The URL of the document. #### Attributes ##### url The URL of the document. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### kind Type identifier, this is available on all parts as a discriminator. **Type:** [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['document-url'\] **Default:** `'document-url'` ##### format The file format of the document. The choice of supported formats were based on the Bedrock Converse API. Other APIs don't require to use a format. **Type:** `DocumentFormat` ### TextContent String content that is tagged with additional metadata. This is useful for including metadata that can be accessed programmatically by the application, but is not sent to the LLM. #### Attributes ##### content The content that is sent to the LLM. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### metadata Additional data that can be accessed programmatically by the application but is not sent to the LLM. **Type:** [`Any`](https://docs.python.org/3/library/typing.html#typing.Any) **Default:** `None` ##### kind Type identifier, this is available on all parts as a discriminator. **Type:** [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['text-content'\] **Default:** `'text-content'` ### BinaryContent Binary content, e.g. an audio or image file. #### Attributes ##### data The binary file data. Use `.base64` to get the base64-encoded string. **Type:** [`bytes`](https://docs.python.org/3/library/stdtypes.html#bytes) ##### media\_type The media type of the binary data. **Type:** `AudioMediaType` | `ImageMediaType` | `DocumentMediaType` | [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### vendor\_metadata Vendor-specific metadata for the file. Supported by: - `GoogleModel`: `BinaryContent.vendor_metadata` is used as `video_metadata`: [https://ai.google.dev/gemini-api/docs/video-understanding#customize-video-processing](https://ai.google.dev/gemini-api/docs/video-understanding#customize-video-processing) - `OpenAIChatModel`, `OpenAIResponsesModel`: `BinaryContent.vendor_metadata['detail']` is used as `detail` setting for images - `XaiModel`: `BinaryContent.vendor_metadata['detail']` is used as `detail` setting for images **Type:** [`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `None` ##### kind Type identifier, this is available on all parts as a discriminator. **Type:** [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['binary'\] **Default:** `'binary'` ##### identifier Identifier for the binary content, such as a unique ID. This identifier can be provided to the model in a message to allow it to refer to this file in a tool call argument, and the tool can look up the file in question by iterating over the message history and finding the matching `BinaryContent`. This identifier is only automatically passed to the model when the `BinaryContent` is returned by a tool. If you're passing the `BinaryContent` as a user message, it's up to you to include a separate text part with the identifier, e.g. "This is file :" preceding the `BinaryContent`. It's also included in inline-text delimiters for providers that require inlining text documents, so the model can distinguish multiple files. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### data\_uri Convert the `BinaryContent` to a data URI. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### base64 Return the binary data as a base64-encoded string. Default encoding is UTF-8. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### is\_audio Return `True` if the media type is an audio type. **Type:** [`bool`](https://docs.python.org/3/library/functions.html#bool) ##### is\_image Return `True` if the media type is an image type. **Type:** [`bool`](https://docs.python.org/3/library/functions.html#bool) ##### is\_video Return `True` if the media type is a video type. **Type:** [`bool`](https://docs.python.org/3/library/functions.html#bool) ##### is\_document Return `True` if the media type is a document type. **Type:** [`bool`](https://docs.python.org/3/library/functions.html#bool) ##### format The file format of the binary content. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) #### Methods ##### narrow\_type `@staticmethod` ```python def narrow_type(bc: BinaryContent) -> BinaryContent | BinaryImage ``` Narrow the type of the `BinaryContent` to `BinaryImage` if it's an image. ###### Returns [`BinaryContent`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.BinaryContent) | [`BinaryImage`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.BinaryImage) ##### from\_data\_uri `@classmethod` ```python def from_data_uri(cls, data_uri: str) -> BinaryContent ``` Create a `BinaryContent` from a data URI. ###### Returns [`BinaryContent`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.BinaryContent) ##### from\_path `@classmethod` ```python def from_path(cls, path: PathLike[str]) -> BinaryContent ``` Create a `BinaryContent` from a path. Defaults to 'application/octet-stream' if the media type cannot be inferred. ###### Returns [`BinaryContent`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.BinaryContent) ###### Raises - `FileNotFoundError` -- if the file does not exist. - `PermissionError` -- if the file cannot be read. ### BinaryImage **Bases:** [`BinaryContent`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.BinaryContent) Binary content that's guaranteed to be an image. ### CachePoint A cache point marker for prompt caching. Can be inserted into UserPromptPart.content to mark cache boundaries. Models that don't support caching will filter these out. Supported by: - Anthropic - Amazon Bedrock (Converse API) #### Attributes ##### kind Type identifier, this is available on all parts as a discriminator. **Type:** [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['cache-point'\] **Default:** `'cache-point'` ##### ttl The cache time-to-live, either "5m" (5 minutes) or "1h" (1 hour). Supported by: - Anthropic -- see [https://docs.claude.com/en/docs/build-with-claude/prompt-caching#1-hour-cache-duration](https://docs.claude.com/en/docs/build-with-claude/prompt-caching#1-hour-cache-duration) for more information. - Amazon Bedrock (Converse API) -- see [https://docs.aws.amazon.com/bedrock/latest/userguide/prompt-caching.html](https://docs.aws.amazon.com/bedrock/latest/userguide/prompt-caching.html) for more information. **Type:** [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['5m', '1h'\] **Default:** `'5m'` ### UploadedFile A reference to a file uploaded to a provider's file storage by ID. This allows referencing files that have been uploaded via provider-specific file APIs rather than providing the file content directly. Supported by: - [`AnthropicModel`](/docs/ai/api/models/anthropic/#pydantic_ai.models.anthropic.AnthropicModel) - [`OpenAIChatModel`](/docs/ai/api/models/openai/#pydantic_ai.models.openai.OpenAIChatModel) - [`OpenAIResponsesModel`](/docs/ai/api/models/openai/#pydantic_ai.models.openai.OpenAIResponsesModel) - [`BedrockConverseModel`](/docs/ai/api/models/bedrock/#pydantic_ai.models.bedrock.BedrockConverseModel) - [`GoogleModel`](/docs/ai/api/models/google/#pydantic_ai.models.google.GoogleModel) (Gemini API: [Files API](https://ai.google.dev/gemini-api/docs/files) URIs, Google Cloud: GCS `gs://` URIs) - [`XaiModel`](/docs/ai/api/models/xai/#pydantic_ai.models.xai.XaiModel) #### Attributes ##### file\_id The provider-specific file identifier. For most providers, this is the file ID returned by the provider's upload API. For GoogleModel (Google Cloud), this must be a GCS URI (`gs://bucket/path`). For GoogleModel (Gemini API), this must be a Google Files API URI (`https://generativelanguage.googleapis.com/...`). For BedrockConverseModel, this must be an S3 URI (`s3://bucket/key`). **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### provider\_name The provider this file belongs to. This is required because file IDs are not portable across providers, and using a file ID with the wrong provider will always result in an error. Tip: Use `model.system` to get the provider name dynamically. **Type:** `UploadedFileProviderName` ##### vendor\_metadata Vendor-specific metadata for the file. The expected shape of this dictionary depends on the provider: Supported by: - `GoogleModel`: used as `video_metadata` for video files **Type:** [`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `None` ##### kind Type identifier, this is available on all parts as a discriminator. **Type:** [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['uploaded-file'\] **Default:** `'uploaded-file'` ##### media\_type Return the media type of the file, inferred from `file_id` if not explicitly provided. Note: Inference relies on the file extension in `file_id`. For opaque file IDs (e.g., `'file-abc123'`), the media type will default to `'application/octet-stream'`. Inference relies on Python's `mimetypes` module, whose results may vary across platforms. Required by some providers (e.g., Bedrock) for certain file types. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### identifier The identifier of the file, such as a unique ID. This identifier can be provided to the model in a message to allow it to refer to this file in a tool call argument, and the tool can look up the file in question by iterating over the message history and finding the matching `UploadedFile`. This identifier is only automatically passed to the model when the `UploadedFile` is returned by a tool. If you're passing the `UploadedFile` as a user message, it's up to you to include a separate text part with the identifier, e.g. "This is file :" preceding the `UploadedFile`. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### format A general-purpose media-type-to-format mapping. Maps media types to format strings (e.g. `'image/png'` -> `'png'`). Covers image, video, audio, and document types. Currently used by Bedrock, which requires explicit format strings. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ### ToolReturn **Bases:** `Generic[_ToolReturnValueT]` A structured tool return that separates the tool result from additional content sent to the model. Can be parameterized with a type to enable return schema generation: - `ToolReturn[User]` -- generates a return schema for `User` - `ToolReturn` (bare) -- no return schema generated #### Attributes ##### return\_value The return value to be used in the tool response. **Type:** `ToolReturnContent` ##### content Content sent to the model as a separate `UserPromptPart`. Use this when you want content to appear outside the tool result message. For multimodal content that should be sent natively in the tool result, return it directly from the tool function or include it in `return_value`. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[`UserContent`\] | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `None` ##### metadata Additional data accessible by the application but not sent to the LLM. **Type:** [`Any`](https://docs.python.org/3/library/typing.html#typing.Any) **Default:** `None` ### UserPromptPart A user prompt, generally written by the end user. Content comes from the `user_prompt` parameter of [`Agent.run`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.AbstractAgent.run), [`Agent.run_sync`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.AbstractAgent.run_sync), and [`Agent.run_stream`](/docs/ai/api/pydantic-ai/agent/#pydantic_ai.agent.AbstractAgent.run_stream). #### Attributes ##### content The content of the prompt. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[`UserContent`\] ##### timestamp The timestamp of the prompt. **Type:** [`datetime`](https://docs.python.org/3/library/datetime.html#module-datetime) **Default:** `field(default_factory=_now_utc)` ##### part\_kind Part type identifier, this is available on all parts as a discriminator. **Type:** [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['user-prompt'\] **Default:** `'user-prompt'` ### BaseToolReturnPart Base class for tool return parts. #### Attributes ##### tool\_name The name of the tool that was called. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### content The tool return content, which may include multimodal files. **Type:** `ToolReturnContent` ##### tool\_call\_id The tool call identifier, this is used by some models including OpenAI. In case the tool call id is not provided by the model, Pydantic AI will generate a random one. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) **Default:** `field(default_factory=_generate_tool_call_id)` ##### tool\_kind Discriminator for the typed subclass of this part (e.g. `'tool-search'`). `None` for any part without a typed subclass -- including all user-defined tools and all native tools without a dedicated typed call/return shape. Subclasses that pin this to a [`ToolPartKind`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.ToolPartKind) literal: - `ToolSearchCallPart` / `ToolSearchReturnPart` -- `'tool-search'` - `NativeToolSearchCallPart` / `NativeToolSearchReturnPart` -- `'tool-search'` **Type:** `ToolPartKind` | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `None` ##### metadata Additional data accessible by the application but not sent to the LLM. **Type:** [`Any`](https://docs.python.org/3/library/typing.html#typing.Any) **Default:** `None` ##### timestamp The timestamp, when the tool returned. **Type:** [`datetime`](https://docs.python.org/3/library/datetime.html#module-datetime) **Default:** `field(default_factory=_now_utc)` ##### outcome The outcome of the tool call. - `'success'`: The tool executed successfully. - `'failed'`: The tool raised an error during execution. - `'denied'`: The tool call was denied by the approval mechanism. **Type:** [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['success', 'failed', 'denied'\] **Default:** `'success'` ##### files The multimodal file parts from `content` (`ImageUrl`, `AudioUrl`, `DocumentUrl`, `VideoUrl`, `BinaryContent`). **Type:** [`list`](https://docs.python.org/3/glossary.html#term-list)\[[`MultiModalContent`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.MultiModalContent)\] #### Methods ##### content\_items ```python def content_items(mode: Literal['raw'] = 'raw') -> list[ToolReturnContent] def content_items(mode: Literal['str']) -> list[str | MultiModalContent] def content_items(mode: Literal['jsonable']) -> list[Any | MultiModalContent] ``` Return content as a flat list for iteration, with optional serialization. ###### Returns [`list`](https://docs.python.org/3/glossary.html#term-list)\[`ToolReturnContent`\] | [`list`](https://docs.python.org/3/glossary.html#term-list)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`MultiModalContent`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.MultiModalContent)\] | [`list`](https://docs.python.org/3/glossary.html#term-list)\[[`Any`](https://docs.python.org/3/library/typing.html#typing.Any) | [`MultiModalContent`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.MultiModalContent)\] ###### Parameters **`mode`** : [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['raw', 'str', 'jsonable'\] _Default:_ `'raw'` Controls serialization of non-file items: - `'raw'`: No serialization. Returns items as-is. - `'str'`: Non-file items are serialized to strings via `tool_return_ta`. File items (`MultiModalContent`) pass through unchanged. - `'jsonable'`: Non-file items are serialized to JSON-compatible Python objects via `tool_return_ta`. File items pass through unchanged. ##### model\_response\_str ```python def model_response_str() -> str ``` Return a string representation of the data content for the model. This excludes multimodal files - use `.files` to get those separately. ###### Returns [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### model\_response\_object ```python def model_response_object() -> dict[str, Any] ``` Return a dictionary representation of the data content, wrapping non-dict types appropriately. This excludes multimodal files - use `.files` to get those separately. Gemini supports JSON dict return values, but no other JSON types, hence we wrap anything else in a dict. ###### Returns [`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] ##### model\_response\_str\_and\_user\_content ```python def model_response_str_and_user_content() -> tuple[str, list[UserContent]] ``` Build a text-only tool result with multimodal files extracted for a trailing user message. For providers whose tool result API only accepts text. Multimodal files are referenced by identifier in the tool result text ('See file {id}.') and included in full in the returned file content list ('This is file {id}:' followed by the file). ###### Returns [`tuple`](https://docs.python.org/3/library/stdtypes.html#tuple)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), [`list`](https://docs.python.org/3/glossary.html#term-list)\[`UserContent`\]\] ##### has\_content ```python def has_content() -> bool ``` Return `True` if the tool return has content. ###### Returns [`bool`](https://docs.python.org/3/library/functions.html#bool) ### ToolReturnPart **Bases:** [`BaseToolReturnPart`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.BaseToolReturnPart) A tool return message, this encodes the result of running a tool. #### Attributes ##### part\_kind Part type identifier, this is available on all parts as a discriminator. **Type:** [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['tool-return'\] **Default:** `'tool-return'` #### Methods ##### narrow\_type `@staticmethod` ```python def narrow_type( part: ToolReturnPart, tool_kind: ToolPartKind | None = None, ) -> ToolReturnPart ``` Promote a base `ToolReturnPart` to its typed subclass when its `tool_kind` is registered. Returns the input unchanged when neither the `tool_kind` kwarg nor `part.tool_kind` resolves to a registered subclass. Pass `tool_kind` to inject the discriminator inline -- the narrower applies it as part of its single dataclass clone, dropping the need for an upstream `replace(part, tool_kind=...)`. Use this on direct construction; Pydantic deserialization promotes automatically via the discriminated-union dispatch on [`ModelRequestPart`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.ModelRequestPart). ###### Returns [`ToolReturnPart`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.ToolReturnPart) ### NativeToolReturnPart **Bases:** [`BaseToolReturnPart`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.BaseToolReturnPart) A tool return message from a native tool. For native tools with a stable cross-provider shape (currently `tool_search`), a `NativeToolReturnPart` may be promoted to a typed subclass like `NativeToolSearchReturnPart` with a narrowed `content` `TypedDict`. See `NativeToolCallPart` for the pattern. #### Attributes ##### provider\_name The name of the provider that generated the response. Required to be set when `provider_details` is set. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `None` ##### provider\_details Additional data returned by the provider that can't be mapped to standard fields. This is used for data that is required to be sent back to APIs, as well as data users may want to access programmatically. When this field is set, `provider_name` is required to identify the provider that generated this data. **Type:** [`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `None` ##### part\_kind Part type identifier, this is available on all parts as a discriminator. **Type:** [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['builtin-tool-return'\] **Default:** `'builtin-tool-return'` #### Methods ##### narrow\_type `@staticmethod` ```python def narrow_type( part: NativeToolReturnPart, tool_kind: ToolPartKind | None = None, ) -> NativeToolReturnPart ``` Promote a base `NativeToolReturnPart` to its typed subclass when its `tool_kind` is registered. Returns the input unchanged when neither the `tool_kind` kwarg nor `part.tool_kind` resolves to a registered subclass. Pass `tool_kind` to inject the discriminator inline -- the narrower applies it as part of its single dataclass clone. Use this on direct construction; Pydantic deserialization promotes automatically via the discriminated-union dispatch on [`ModelResponsePart`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.ModelResponsePart). ###### Returns [`NativeToolReturnPart`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.NativeToolReturnPart) ### RetryPromptPart A message back to a model asking it to try again. This can be sent for a number of reasons: - Pydantic validation of tool arguments failed, here content is derived from a Pydantic [`ValidationError`](https://docs.pydantic.dev/latest/api/pydantic-core/pydantic_core/#pydantic_core.ValidationError) - a tool raised a [`ModelRetry`](/docs/ai/api/pydantic-ai/exceptions/#pydantic_ai.exceptions.ModelRetry) exception - no tool was found for the tool name - the model returned plain text when a structured response was expected - Pydantic validation of a structured response failed, here content is derived from a Pydantic [`ValidationError`](https://docs.pydantic.dev/latest/api/pydantic-core/pydantic_core/#pydantic_core.ValidationError) - an output validator raised a [`ModelRetry`](/docs/ai/api/pydantic-ai/exceptions/#pydantic_ai.exceptions.ModelRetry) exception #### Attributes ##### content Details of why and how the model should retry. If the retry was triggered by a [`ValidationError`](https://docs.pydantic.dev/latest/api/pydantic-core/pydantic_core/#pydantic_core.ValidationError), this will be a list of error details. **Type:** [`list`](https://docs.python.org/3/glossary.html#term-list)\[[`pydantic_core.ErrorDetails`](https://docs.pydantic.dev/latest/api/pydantic-core/pydantic_core/#pydantic_core.ErrorDetails)\] | [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### tool\_name The name of the tool that was called, if any. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `None` ##### tool\_call\_id The tool call identifier, this is used by some models including OpenAI. In case the tool call id is not provided by the model, Pydantic AI will generate a random one. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) **Default:** `field(default_factory=_generate_tool_call_id)` ##### timestamp The timestamp, when the retry was triggered. **Type:** [`datetime`](https://docs.python.org/3/library/datetime.html#module-datetime) **Default:** `field(default_factory=_now_utc)` ##### part\_kind Part type identifier, this is available on all parts as a discriminator. **Type:** [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['retry-prompt'\] **Default:** `'retry-prompt'` #### Methods ##### model\_response ```python def model_response() -> str ``` Return a string message describing why the retry is requested. ###### Returns [`str`](https://docs.python.org/3/library/stdtypes.html#str) ### InstructionPart A single instruction block with metadata about its origin. Instructions are composed of one or more parts, each of which can be static (from a literal string) or dynamic (from a function, template, or toolset). This distinction allows model implementations to make intelligent caching decisions -- e.g. Anthropic's prompt caching can cache the static prefix while leaving dynamic instructions uncached. #### Attributes ##### content The text content of this instruction block. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### dynamic Whether this instruction came from a dynamic source (function, template, or toolset). Static instructions (`dynamic=False`) come from literal strings passed to `Agent(instructions=...)`. Dynamic instructions (`dynamic=True`) come from `@agent.instructions` functions, `TemplateStr`, or toolset `get_instructions()` methods. **Type:** [`bool`](https://docs.python.org/3/library/functions.html#bool) **Default:** `False` ##### part\_kind Part type identifier, used as a discriminator for deserialization. **Type:** [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['instruction'\] **Default:** `'instruction'` #### Methods ##### join `@staticmethod` ```python def join(parts: Sequence[InstructionPart]) -> str | None ``` Join instruction parts into a single string, separated by double newlines. ###### Returns [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) ##### sorted `@staticmethod` ```python def sorted(parts: Sequence[InstructionPart]) -> list[InstructionPart] ``` Sort instruction parts with static (`dynamic=False`) before dynamic, preserving relative order. ###### Returns [`list`](https://docs.python.org/3/glossary.html#term-list)\[[`InstructionPart`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.InstructionPart)\] ### ModelRequest A request generated by Pydantic AI and sent to a model, e.g. a message from the Pydantic AI app to the model. #### Attributes ##### parts The parts of the user message. **Type:** [`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)\[[`ModelRequestPart`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.ModelRequestPart)\] ##### timestamp The timestamp when the request was sent to the model. **Type:** [`datetime`](https://docs.python.org/3/library/datetime.html#module-datetime) | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `None` ##### instructions The instructions string for this request, rendered from structured instruction parts. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `None` ##### kind Message type identifier, this is available on all parts as a discriminator. **Type:** [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['request'\] **Default:** `'request'` ##### run\_id The unique identifier of the agent run in which this message originated. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `None` ##### conversation\_id The unique identifier of the conversation this message belongs to. A conversation spans potentially multiple agent runs that share message history. Emitted as the `gen_ai.conversation.id` OpenTelemetry span attribute on the agent run. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `None` ##### metadata Additional data that can be accessed programmatically by the application but is not sent to the LLM. **Type:** [`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `None` #### Methods ##### user\_text\_prompt `@classmethod` ```python def user_text_prompt( cls, user_prompt: str, instructions: str | None = None, ) -> ModelRequest ``` Create a `ModelRequest` with a single user prompt as text. ###### Returns [`ModelRequest`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.ModelRequest) ### TextPart A plain text response from a model. #### Attributes ##### content The text content of the response. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### id An optional identifier of the text part. When this field is set, `provider_name` is required to identify the provider that generated this data. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `None` ##### provider\_name The name of the provider that generated the response. Required to be set when `provider_details` or `id` is set. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `None` ##### provider\_details Additional data returned by the provider that can't be mapped to standard fields. This is used for data that is required to be sent back to APIs, as well as data users may want to access programmatically. When this field is set, `provider_name` is required to identify the provider that generated this data. **Type:** [`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `None` ##### part\_kind Part type identifier, this is available on all parts as a discriminator. **Type:** [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['text'\] **Default:** `'text'` #### Methods ##### has\_content ```python def has_content() -> bool ``` Return `True` if the text content is non-empty. ###### Returns [`bool`](https://docs.python.org/3/library/functions.html#bool) ### ThinkingPart A thinking response from a model. #### Attributes ##### content The thinking content of the response. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### id The identifier of the thinking part. When this field is set, `provider_name` is required to identify the provider that generated this data. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `None` ##### signature The signature of the thinking. Supported by: - Anthropic (corresponds to the `signature` field) - Bedrock (corresponds to the `signature` field) - Google (corresponds to the `thought_signature` field) - OpenAI (corresponds to the `encrypted_content` field) When this field is set, `provider_name` is required to identify the provider that generated this data. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `None` ##### provider\_name The name of the provider that generated the response. Signatures are only sent back to the same provider. Required to be set when `provider_details`, `id` or `signature` is set. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `None` ##### provider\_details Additional data returned by the provider that can't be mapped to standard fields. This is used for data that is required to be sent back to APIs, as well as data users may want to access programmatically. When this field is set, `provider_name` is required to identify the provider that generated this data. **Type:** [`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `None` ##### part\_kind Part type identifier, this is available on all parts as a discriminator. **Type:** [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['thinking'\] **Default:** `'thinking'` #### Methods ##### has\_content ```python def has_content() -> bool ``` Return `True` if the thinking content is non-empty. ###### Returns [`bool`](https://docs.python.org/3/library/functions.html#bool) ### CompactionPart A compaction part that summarizes previous conversation history. Compaction parts contain an opaque or readable summary of prior messages, produced by provider-specific compaction mechanisms. They must be round-tripped back to the same provider in subsequent requests. For Anthropic, `content` contains a readable text summary. For OpenAI, `content` is `None` and the encrypted data is stored in `provider_details`. #### Attributes ##### content The compaction summary text, if available. For Anthropic: a readable text summary of compacted messages. For OpenAI: `None` (the compacted content is encrypted and stored in `provider_details`). **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `None` ##### id The identifier of the compaction part. When this field is set, `provider_name` is required to identify the provider that generated this data. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `None` ##### provider\_name The name of the provider that generated the compaction. Compaction data is only sent back to the same provider. Required to be set when `provider_details` or `id` is set. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `None` ##### provider\_details Additional data returned by the provider that can't be mapped to standard fields. For OpenAI: contains `encrypted_content` and other fields from `ResponseCompactionItem`. When this field is set, `provider_name` is required to identify the provider that generated this data. **Type:** [`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `None` ##### part\_kind Part type identifier, this is available on all parts as a discriminator. **Type:** [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['compaction'\] **Default:** `'compaction'` #### Methods ##### has\_content ```python def has_content() -> bool ``` Return `True` if the compaction content is non-empty. ###### Returns [`bool`](https://docs.python.org/3/library/functions.html#bool) ### FilePart A file response from a model. #### Attributes ##### content The file content of the response. **Type:** [`Annotated`](https://docs.python.org/3/library/typing.html#typing.Annotated)\[[`BinaryContent`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.BinaryContent), [`pydantic.AfterValidator`](https://docs.pydantic.dev/latest/api/pydantic/functional_validators/#pydantic.functional_validators.AfterValidator)(`BinaryImage.narrow_type`)\] ##### id The identifier of the file part. When this field is set, `provider_name` is required to identify the provider that generated this data. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `None` ##### provider\_name The name of the provider that generated the response. Required to be set when `provider_details` or `id` is set. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `None` ##### provider\_details Additional data returned by the provider that can't be mapped to standard fields. This is used for data that is required to be sent back to APIs, as well as data users may want to access programmatically. When this field is set, `provider_name` is required to identify the provider that generated this data. **Type:** [`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `None` ##### part\_kind Part type identifier, this is available on all parts as a discriminator. **Type:** [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['file'\] **Default:** `'file'` #### Methods ##### has\_content ```python def has_content() -> bool ``` Return `True` if the file content is non-empty. ###### Returns [`bool`](https://docs.python.org/3/library/functions.html#bool) ### BaseToolCallPart A tool call from a model. #### Attributes ##### tool\_name The name of the tool to call. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### args The arguments to pass to the tool. This is stored either as a JSON string or a Python dictionary depending on how data was received. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `None` ##### tool\_call\_id The tool call identifier, this is used by some models including OpenAI. In case the tool call id is not provided by the model, Pydantic AI will generate a random one. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) **Default:** `field(default_factory=_generate_tool_call_id)` ##### tool\_kind Discriminator for the typed subclass of this part (e.g. `'tool-search'`). See [`BaseToolReturnPart.tool_kind`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.BaseToolReturnPart.tool_kind) for the full semantics. **Type:** `ToolPartKind` | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `None` ##### id An optional identifier of the tool call part, separate from the tool call ID. This is used by some APIs like OpenAI Responses. When this field is set, `provider_name` is required to identify the provider that generated this data. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `None` ##### provider\_name The name of the provider that generated the response. Native tool calls are only sent back to the same provider. Required to be set when `provider_details` or `id` is set. **Type:** [`str`](https://docs.python.org/3/library/stdtypes.html#str) | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `None` ##### provider\_details Additional data returned by the provider that can't be mapped to standard fields. This is used for data that is required to be sent back to APIs, as well as data users may want to access programmatically. When this field is set, `provider_name` is required to identify the provider that generated this data. **Type:** [`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] | [`None`](https://docs.python.org/3/library/constants.html#None) **Default:** `None` #### Methods ##### args\_as\_dict ```python def args_as_dict(raise_if_invalid: bool = False) -> dict[str, Any] ``` Return the arguments as a Python dictionary. This is just for convenience with models that require dicts as input. ###### Returns [`dict`](https://docs.python.org/3/reference/expressions.html#dict)\[[`str`](https://docs.python.org/3/library/stdtypes.html#str), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)\] ###### Parameters **`raise_if_invalid`** : [`bool`](https://docs.python.org/3/library/functions.html#bool) _Default:_ `False` If `True`, a `ValueError` or `AssertionError` caused by malformed JSON in `args` will be re-raised. When `False` (the default), malformed JSON is handled gracefully by returning `{'INVALID_JSON': ''}` so that the value can still be sent to a model API (e.g. during a retry flow) without crashing. ##### args\_as\_json\_str ```python def args_as_json_str() -> str ``` Return the arguments as a JSON string. This is just for convenience with models that require JSON strings as input. ###### Returns [`str`](https://docs.python.org/3/library/stdtypes.html#str) ##### has\_content ```python def has_content() -> bool ``` Return `True` if the tool call has content. ###### Returns [`bool`](https://docs.python.org/3/library/functions.html#bool) ### ToolCallPart **Bases:** [`BaseToolCallPart`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.BaseToolCallPart) A tool call from a model. #### Attributes ##### part\_kind Part type identifier, this is available on all parts as a discriminator. Note that this is different from `ToolCallPartDelta.part_delta_kind`. **Type:** [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal)\['tool-call'\] **Default:** `'tool-call'` #### Methods ##### narrow\_type `@staticmethod` ```python def narrow_type( part: ToolCallPart, tool_kind: ToolPartKind | None = None, ) -> ToolCallPart ``` Promote a base `ToolCallPart` to its typed subclass when its `tool_kind` is registered. Returns the input unchanged when neither the `tool_kind` kwarg nor `part.tool_kind` resolves to a registered subclass. Pass `tool_kind` to inject the discriminator inline -- the narrower applies it as part of its single dataclass clone, dropping the need for an upstream `replace(part, tool_kind=...)`. Use this on direct construction; Pydantic deserialization promotes automatically via the discriminated-union dispatch on [`ModelResponsePart`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.ModelResponsePart). ###### Returns [`ToolCallPart`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.ToolCallPart) ### NativeToolCallPart **Bases:** [`BaseToolCallPart`](/docs/ai/api/pydantic-ai/messages/#pydantic_ai.messages.BaseToolCallPart) A tool call to a native tool. For native tools with a stable cross-provider shape (currently `tool_search`), this base class can be promoted to a typed subclass with a narrowed `args` `TypedDict`. See `NativeToolSearchCallPart` for the canonical example. Adding a typed subclass for a future native tool (see `pydantic_ai._tool_search` for a worked example): 1. Add a sibling `pydantic_ai/_.py` module that defines the cross-provider `TypedDict`s, the `NativeToolCallPart` / `NativeToolReturnPart` subclasses, and registers their narrowers into `_NATIVE_CALL_NARROWERS` / `_NATIVE_RETURN_NARROWERS` keyed by `tool_kind`. Subclass overrides `tool_kind: Literal['']` to match the emitting [`AbstractNativeTool.kind`](/docs/ai/api/pydantic-ai/native_tools/#pydantic_ai.native_tools.AbstractNativeTool.kind), and shadows `args` / `content` with a narrower type. 2. Late-import the new module from this file (alongside the existing tool-search import) so registration runs whenever `pydantic_ai.messages` is imported. 3. Add the subclass to `ModelResponsePart`'s discriminated union and to `_model_response_part_discriminator` so Pydantic deserialization auto-promotes on `model_validate` / `model_validate_json`. Dispatch is by `tool_kind`, not `tool_name`. This protects users whose tools happen to share a name with one of ours from accidentally getting their parts promoted (and failing shape validation against the typed `args`/`content`). The `provider_details` field carries genuinely non-portable provider extras (e.g. Anthropic's `strategy: 'bm25' | 'regex'` for tool search). Promote a field to a typed slot in `args` / `content` only when at least two of OpenAI, Anthropic, and Google support it (cf. [issue #3885](https://github.com/pydantic/pydantic-ai/issues/3885)). MCP server tools land here with `tool_kind='mcp_server'` (label stays in `tool_name='mcp_server: