Skip to content

Commit ea65d87

Browse files
xflashxxKludex
andauthored
Improve Dynamic Instructions Documentation (#1819)
Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>
1 parent a25eb96 commit ea65d87

File tree

1 file changed

+25
-21
lines changed

1 file changed

+25
-21
lines changed

docs/agents.md

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -682,49 +682,53 @@ You should use:
682682

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

685-
```python {title="instructions.py"}
686-
from pydantic_ai import Agent
685+
Instructions, like system prompts, fall into two categories:
687686

688-
agent = Agent(
689-
'openai:gpt-4o',
690-
instructions='You are a helpful assistant that can answer questions and help with tasks.', # (1)!
691-
)
687+
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__].
688+
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.
692689

693-
result = agent.run_sync('What is the capital of France?')
694-
print(result.output)
695-
#> Paris
696-
```
690+
Both static and dynamic instructions can be added to a single agent, and they are appended in the order they are defined at runtime.
697691

698-
1. This will be the only instructions for this agent.
692+
Here's an example using both types of instructions:
699693

700-
_(This example is complete, it can be run "as is")_
701-
702-
You can also dynamically change the instructions for an agent by using the `@agent.instructions` decorator.
703-
704-
Note that returning an empty string will result in no instruction message added.
705-
706-
```python {title="dynamic_instructions.py"}
694+
```python {title="instructions.py"}
707695
from datetime import date
708696

709697
from pydantic_ai import Agent, RunContext
710698

711-
agent = Agent('openai:gpt-4o', deps_type=str)
699+
agent = Agent(
700+
'openai:gpt-4o',
701+
deps_type=str, # (1)!
702+
instructions="Use the customer's name while replying to them.", # (2)!
703+
)
712704

713705

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

718710

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

715+
723716
result = agent.run_sync('What is the date?', deps='Frank')
724717
print(result.output)
725718
#> Hello Frank, the date today is 2032-01-02.
726719
```
727720

721+
1. The agent expects a string dependency.
722+
2. Static instructions defined at agent creation time.
723+
3. Dynamic instructions defined via a decorator with [`RunContext`][pydantic_ai.tools.RunContext],
724+
this is called just after `run_sync`, not when the agent is created, so can benefit from runtime
725+
information like the dependencies used on that run.
726+
4. Another dynamic instruction, instructions don't have to have the `RunContext` parameter.
727+
728+
_(This example is complete, it can be run "as is")_
729+
730+
Note that returning an empty string will result in no instruction message added.
731+
728732
## Reflection and self-correction
729733

730734
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.

0 commit comments

Comments
 (0)