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/message-history.md
+10Lines changed: 10 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -334,6 +334,10 @@ custom processing logic.
334
334
Pydantic AI provides a `history_processors` parameter on `Agent` that allows you to intercept and modify
335
335
the message history before each model request.
336
336
337
+
!!! warning "History processors replace the message history"
338
+
History processors replace the message history in the state with the processed messages, including the new user prompt part.
339
+
This means that if you want to keep the original message history, you need to make a copy of it.
340
+
337
341
### Usage
338
342
339
343
The `history_processors` is a list of callables that take a list of
@@ -389,6 +393,9 @@ long_conversation_history: list[ModelMessage] = [] # Your long conversation his
389
393
# result = agent.run_sync('What did we discuss?', message_history=long_conversation_history)
390
394
```
391
395
396
+
!!! warning "Be careful when slicing the message history"
397
+
When slicing the message history, you need to make sure that tool calls and returns are paired, otherwise the LLM may return an error. For more details, refer to [this GitHub issue](https://github.com/pydantic/pydantic-ai/issues/2050#issuecomment-3019976269).
398
+
392
399
#### `RunContext` parameter
393
400
394
401
History processors can optionally accept a [`RunContext`][pydantic_ai.tools.RunContext] parameter to access
!!! warning "Be careful when summarizing the message history"
460
+
When summarizing the message history, you need to make sure that tool calls and returns are paired, otherwise the LLM may return an error. For more details, refer to [this GitHub issue](https://github.com/pydantic/pydantic-ai/issues/2050#issuecomment-3019976269), where you can find examples of summarizing the message history.
461
+
452
462
### Testing History Processors
453
463
454
464
You can test what messages are actually sent to the model provider using
0 commit comments