You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/agents.md
+25-21Lines changed: 25 additions & 21 deletions
Original file line number
Diff line number
Diff line change
@@ -682,49 +682,53 @@ You should use:
682
682
683
683
In general, we recommend using `instructions` instead of `system_prompt` unless you have a specific reason to use `system_prompt`.
684
684
685
-
```python {title="instructions.py"}
686
-
from pydantic_ai import Agent
685
+
Instructions, like system prompts, fall into two categories:
687
686
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.
692
689
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.
697
691
698
-
1. This will be the only instructions for this agent.
692
+
Here's an example using both types of instructions:
699
693
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"}
707
695
from datetime import date
708
696
709
697
from pydantic_ai import Agent, RunContext
710
698
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)!
result = agent.run_sync('What is the date?', deps='Frank')
724
717
print(result.output)
725
718
#> Hello Frank, the date today is 2032-01-02.
726
719
```
727
720
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
+
728
732
## Reflection and self-correction
729
733
730
734
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