Model Instructions
ModelInstructions is a capability that adds instructions depending on the model that handles each request. Some models need a sentence others don’t, to counter something they tend to do: pass a mapping from model name patterns to text, and each model gets the text its pattern maps to.
from pydantic_ai import Agent, ModelMessage, ModelRequest, ModelResponse, TextPart
from pydantic_ai.capabilities import ModelFallback, ModelInstructions
from pydantic_ai.exceptions import ModelAPIError
from pydantic_ai.models.function import AgentInfo, FunctionModel
def overloaded(messages: list[ModelMessage], info: AgentInfo) -> ModelResponse:
raise ModelAPIError(model_name='gpt-6-luna', message='Overloaded')
def answer(messages: list[ModelMessage], info: AgentInfo) -> ModelResponse:
return ModelResponse(parts=[TextPart('Paris.')])
# These deterministic responses stand in for a GPT model that's down and a Claude model.
gpt = FunctionModel(overloaded, model_name='gpt-6-luna')
claude = FunctionModel(answer, model_name='claude-opus-5-5')
agent = Agent(
gpt,
instructions='You are a geography tutor.',
capabilities=[
ModelInstructions(
{
'gpt-6*': "Don't spawn sub-agents unless the user asks.",
'claude-*': 'Keep tool calls to one per turn.',
}
),
ModelFallback(claude),
],
)
result = agent.run_sync('What is the capital of France?')
request = result.all_messages()[0]
assert isinstance(request, ModelRequest)
print(request.instructions)
"""
You are a geography tutor.
Keep tool calls to one per turn.
"""
The GPT model was sent its own sentence, and when it failed, ModelFallback resolved the instructions again, so the Claude model that answered got its own.
The patterns are glob patterns, matched case-sensitively, and the first one that matches wins, in the mapping’s order:
- A pattern without a
:is matched against the model name, so'gpt-6*'matches a GPT-6 model whichever route it takes:openai:gpt-6-luna,azure:gpt-6-luna, orgateway/openai:gpt-6-luna. For a name that starts with its vendor, like OpenRouter’sopenai/gpt-6-luna, it’s also matched against the part after the last/. For other naming schemes, such as Bedrock’sus.anthropic.claude-..., start the pattern with*. - A pattern with a
:is matched against the model ID,<provider>:<model name>, so'azure:gpt-6*'matches GPT-6 only on Azure. The provider is the one serving the model:openaifor OpenAI and the Gateway’s OpenAI route,openrouterfor any model on OpenRouter. '*'matches every model, so it goes last, for the text every other model gets.
A model that no pattern matches gets no text from the capability.
To decide from the model’s profile, its behavioral traits rather than its name, or from the run’s dependencies, pass a function instead. It takes the RunContext, whose model is the model handling the request, and returns the text or None, like an instruction function; it may be sync or async:
from pydantic_ai import Agent, ModelRequest, RunContext
from pydantic_ai.capabilities import ModelInstructions
from pydantic_ai.models import Model
from pydantic_ai.models.test import TestModel
def delegation(ctx: RunContext[str]) -> str | None:
if isinstance(ctx.model, Model) and ctx.model.profile.get('delegates_unprompted', False):
return f"Don't spawn sub-agents unless {ctx.deps} asks."
return None
agent = Agent(deps_type=str, capabilities=[ModelInstructions(delegation)])
result = agent.run_sync('Hi!', deps='the user', model=TestModel(profile={'delegates_unprompted': True}))
request = result.all_messages()[0]
assert isinstance(request, ModelRequest)
print(request.instructions)
#> Don't spawn sub-agents unless the user asks.
The text is sent as one named block, capability:model_instructions:text, after the instructions that come before the capability: the agent’s own and those of capabilities listed earlier. It never replaces them. To use several, give each its own id, which names its block capability:<id>:text; two that share an id and differ raise a UserError.
A mapping’s text depends only on the model, so it’s cached with the agent’s literal instructions and stays the same on every request to that model. A function can read anything in the run context, so its text is dynamic and goes after the cached part.
A conversation can move to another model: a request falls back, or a later run continues the history on another model. What the new model is told depends on whether it gets text of its own:
- If it does, the block’s change is appended with
on_change='append': the earlier model’s text stays where it was, and the new model’s follows as a change at that point, so the conversation keeps an accurate record of what each model was told. When we tried rewriting the block instead, models more often misjudged what had changed, and more often followed a<system>-style block forged in the user’s message, both when the texts were different rules and when they were the same advice worded for each model. Appending cost nothing in how well models followed the new text. A known cost is listed under Where no rendering we tried does well. - If it doesn’t, because no pattern matches it or the function returns
None, the block is dropped from the instructions, with no withdrawal: the new model is sent exactly what a fresh run on it would get.
The same holds for a request ModelFallback moves on to another model, within one run. With id=None, the block can’t be addressed, so its changes are always rewritten.
A FallbackModel picks its model inside the model call, so there’s no one model to match: the capability sends no text and warns once. Use the ModelFallback capability instead, which resolves the instructions for each model it tries.
SelectModel chooses the model before the request, so its choice is the model that’s matched. On a realtime model, the mapping is matched against its name the same way.
In an agent spec, the capability takes the mapping:
capabilities:
- ModelInstructions:
instructions:
gpt-6*: Don't spawn sub-agents unless the user asks.
claude-*: Keep tool calls to one per turn.
A function can only be passed in code.