-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Custom model prompt templates #3656
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
base: main
Are you sure you want to change the base?
Custom model prompt templates #3656
Conversation
|
@DouweM Please take a look whenever you get some time, I have not added a retry-kind on RetryPromptPart because regardless of the way we reach we do end up with the same default string at the end that is "Fix and try again". So I think covering this does the job for us. Would you say knowing why this particular RetryPromptPart is being fired off(with a separate field) is worth it on its own as well, I can take that up as a separate PR or in this one as well if we think there is enough use case to warrant it. |
Add
return_kindfield and customizable prompt templatesThis PR introduces two major changes to PydanticAI's messaging and agent configuration system:
1.
return_kindfield onToolReturnPartAdded a new
return_kindfield toToolReturnPartthat disambiguates how a tool call was resolved:'tool-executed': Tool ran successfully and produced a return value'final-result-processed': An output tool produced the run's final result'output-tool-not-executed': An output tool was skipped because a final result already existed'function-tool-not-executed': A function tool was skipped due to early termination after a final result'tool-denied': The tool call was rejected by an approval handler2. Customizable Prompt Templates
Introduced a new
PromptTemplatesclass that allows users to customize system-injected messages that PydanticAI sends to models:Each template can be either:
(part, RunContext) -> strfor dynamic, context-aware messagesTemplates available:
retry_prompt: Sent after validation failures or invalid responsesfinal_result_processed: Confirmation when a final result is successfully processedoutput_tool_not_executed: Message when an output tool call is skippedfunction_tool_not_executed: Message when a function tool call is skippedIntegration points:
The
prompt_templatesparameter has been added throughout the agent system:Agent(..., prompt_templates=...)agent.run(..., prompt_templates=...)- allows per-run overridesrun(),run_sync(),run_stream(),run_stream_events(),iter()agent.override(prompt_templates=...)Agent,WrappedAgent,DBOSAgent,PrefectAgent,TemporalAgentTemplates are applied right before messages are sent to the model, with
RetryPromptPartstoring the compiled template in its newretry_templatefield.