Skip to content

Improve Dynamic Instructions Documentation #1819

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 23 additions & 19 deletions docs/agents.md
Original file line number Diff line number Diff line change
Expand Up @@ -682,47 +682,51 @@ You should use:

In general, we recommend using `instructions` instead of `system_prompt` unless you have a specific reason to use `system_prompt`.

```python {title="instructions.py"}
from pydantic_ai import Agent

agent = Agent(
'openai:gpt-4o',
instructions='You are a helpful assistant that can answer questions and help with tasks.', # (1)!
)
Instructions, like system prompts, fall into two categories:

result = agent.run_sync('What is the capital of France?')
print(result.output)
#> Paris
```

1. This will be the only instructions for this agent.
1. **Static instructions**: These are known when writing the code and can be defined via the `instructions` parameter of the [`Agent` constructor][pydantic_ai.Agent.__init__].
2. **Dynamic instructions**: These rely on context that is only available at runtime and should be defined using functions decorated with [`@agent.instructions`][pydantic_ai.Agent.instructions]. Unlike dynamic system prompts, which may be reused when `message_history` is present, dynamic instructions are always reevaluated.

_(This example is complete, it can be run "as is")_
Both static and dynamic instructions can be added to a single agent, and they are appended in the order they are defined at runtime.

You can also dynamically change the instructions for an agent by using the `@agent.instructions` decorator.
Here's an example using both types of instructions:

```python {title="dynamic_instructions.py"}
```python {title="instructions.py"}
from datetime import date

from pydantic_ai import Agent, RunContext

agent = Agent('openai:gpt-4o', deps_type=str)
agent = Agent(
'openai:gpt-4o',
deps_type=str, # (1)!
instructions="Use the customer's name while replying to them.", # (2)!
)


@agent.instructions
@agent.instructions # (3)!
def add_the_users_name(ctx: RunContext[str]) -> str:
return f"The user's name is {ctx.deps}."


@agent.instructions
def add_the_date() -> str:
def add_the_date() -> str: # (4)!
return f'The date is {date.today()}.'


result = agent.run_sync('What is the date?', deps='Frank')
print(result.output)
#> Hello Frank, the date today is 2032-01-02.
```

1. The agent expects a string dependency.
2. Static instructions defined at agent creation time.
3. Dynamic instructions defined via a decorator with [`RunContext`][pydantic_ai.tools.RunContext], this is called just after `run_sync`, not when the agent is created, so can benefit from runtime information like the dependencies used on that run.
4. Another dynamic instruction, instructions don't have to have the `RunContext` parameter.

_(This example is complete, it can be run "as is")_



## Reflection and self-correction

Validation errors from both function tool parameter validation and [structured output validation](output.md#structured-output) can be passed back to the model with a request to retry.
Expand Down