-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Automatically use Responses API previous_response_id
instead of sending message history when possible
#2756
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
GDaamn
wants to merge
9
commits into
pydantic:main
Choose a base branch
from
GDaamn:feat/add-support-previous_response_id
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
9c0d7fd
add support for previous_response_id from Responses API
GDaamn 8e79c33
update link
GDaamn 96a9118
update logic and docs
GDaamn e679a67
update import in test
GDaamn 3029a95
fix test
GDaamn d327750
add fixes for types
GDaamn 5d9a846
Update docs
GDaamn 16c19c7
update conditional check
GDaamn 8351ebe
update conditional check
GDaamn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
tests/models/cassettes/test_openai_responses/test_openai_previous_response_id.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
interactions: | ||
- request: | ||
headers: | ||
accept: | ||
- application/json | ||
accept-encoding: | ||
- gzip, deflate | ||
connection: | ||
- keep-alive | ||
content-type: | ||
- application/json | ||
host: | ||
- api.openai.com | ||
method: POST | ||
parsed_body: | ||
input: | ||
- content: The secret key is sesame | ||
role: user | ||
instructions: '' | ||
model: gpt-5 | ||
text: | ||
format: | ||
type: text | ||
uri: https://api.openai.com/v1/responses | ||
response: | ||
headers: | ||
content-type: | ||
- application/json | ||
parsed_body: | ||
created_at: 1743075629 | ||
error: null | ||
id: resp_1234 | ||
incomplete_details: null | ||
instructions: '' | ||
max_output_tokens: null | ||
metadata: {} | ||
model: gpt-5 | ||
object: response | ||
output: | ||
- content: | ||
- annotations: [] | ||
text: "Open sesame! What would you like to unlock?" | ||
type: output_text | ||
id: msg_test_previous_response_id | ||
role: assistant | ||
status: completed | ||
type: message | ||
parallel_tool_calls: true | ||
previous_response_id: null | ||
reasoning: null | ||
status: complete | ||
status_details: null | ||
tool_calls: null | ||
total_tokens: 15 | ||
usage: | ||
input_tokens: 10 | ||
input_tokens_details: | ||
cached_tokens: 0 | ||
output_tokens: 1 | ||
output_tokens_details: | ||
reasoning_tokens: 0 | ||
total_tokens: 11 | ||
status: | ||
code: 200 | ||
message: OK | ||
- request: | ||
headers: | ||
accept: | ||
- application/json | ||
accept-encoding: | ||
- gzip, deflate | ||
connection: | ||
- keep-alive | ||
content-type: | ||
- application/json | ||
host: | ||
- api.openai.com | ||
method: POST | ||
parsed_body: | ||
input: | ||
- content: What is the secret key again? | ||
role: user | ||
instructions: '' | ||
model: gpt-5 | ||
text: | ||
format: | ||
type: text | ||
previous_response_id: resp_1234 | ||
uri: https://api.openai.com/v1/responses | ||
response: | ||
headers: | ||
content-type: | ||
- application/json | ||
parsed_body: | ||
created_at: 1743075630 | ||
error: null | ||
id: resp_5678 | ||
incomplete_details: null | ||
instructions: '' | ||
max_output_tokens: null | ||
metadata: {} | ||
model: gpt-5 | ||
object: response | ||
output: | ||
- content: | ||
- annotations: [] | ||
text: "sesame" | ||
type: output_text | ||
id: msg_test_previous_response_id | ||
role: assistant | ||
status: completed | ||
type: message | ||
parallel_tool_calls: true | ||
previous_response_id: resp_1234 | ||
reasoning: null | ||
status: complete | ||
status_details: null | ||
tool_calls: null | ||
total_tokens: 15 | ||
usage: | ||
input_tokens: 10 | ||
input_tokens_details: | ||
cached_tokens: 0 | ||
output_tokens: 1 | ||
output_tokens_details: | ||
reasoning_tokens: 0 | ||
total_tokens: 11 | ||
status: | ||
code: 200 | ||
message: OK | ||
version: 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should account for https://ai.pydantic.dev/message-history/#processing-message-history. It would be unexpected if that feature is ignored entirely when using the Responses API. Maybe we should not automatically use
previous_response_id
if a history processor was configured?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Additionally, https://platform.openai.com/docs/guides/reasoning?api-mode=responses#encrypted-reasoning-items mentions the Responses API "stateless mode (either with store set to false, or when an organization is enrolled in zero data retention)", in which case
previous_response_id
wouldn't work. So there needs to be a way to turn it off.Perhaps it's better to make it opt-in by setting
OpenAIModelSettings(openai_previous_response_id='auto')
or something?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
history_processor
, I did some quick experiments and it seems it isn't intefering. Also checking the code I can see that the the history is processed inside_prepare_request
, a step before we pass themessages
to model request where theprevious_response_id
logic happens.pydantic-ai/pydantic_ai_slim/pydantic_ai/_agent_graph.py
Lines 422 to 442 in 3c2f1cf
Please lmk if there are other scenarios where this is not the case or I misunderstood the flow
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
to this right ?
-> when user passes
message_history
, it will be trimmed only whenopenai_previous_response_id = 'auto'
and theModelResponses
are from the same model.But in this case, we'll still have to explicitly set
openai_previous_response_id = 'auto'
, when mutliple requests are made within the sameagent.run
(like part of a tool call etc).