Skip to content

Save session on turn rather than at final response - #1550

Merged
seratch merged 4 commits into
openai:mainfrom
spokeydokeys:save-session-on-turn
Sep 9, 2025
Merged

Save session on turn rather than at final response#1550
seratch merged 4 commits into
openai:mainfrom
spokeydokeys:save-session-on-turn

Conversation

@spokeydokeys

@spokeydokeys spokeydokeys commented Aug 21, 2025

Copy link
Copy Markdown
Contributor

The current implementation of sessions saves the session on the final output. This works as a "memory" of previous full conversations, but does not work if you need access to the session during the turn. A use case for this is providing access to the full conversation to an agent-as-tool during the turn.

This PR updates the run and _start_streaming methods in run.py to save the session on each turn.

The basic flow is:

  • On completion start, save the input into the session.
  • After each turn, add only the turn results to the session.

Also included is an example file that shows the use of this pattern. It is an adapted version of the agents-as-tools example where a check agent is called within the same turn as the translation agents and examines their outputs via the conversation history, rather than as a generated input.

  • Current tests pass and I don't think an update to the tests is needed for this PR.
  • Documentation about sessions did not cover this case, but I felt it was implied that it would work. I think these changes bring the behavior in line with the documentation.
  • I have added an example that demonstrates it in action.

Resolves #1551

@spokeydokeys

Copy link
Copy Markdown
Contributor Author

Addresses issue #1551

@seratch seratch added bug Something isn't working feature:sessions labels Aug 21, 2025
@seratch
seratch requested a review from rm-openai August 21, 2025 21:20

@seratch seratch left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for taking the time to send this. At a glance, the changes look good, but we need unit tests verifying the patterns and we don't need the additional example.

@@ -0,0 +1,116 @@
import asyncio

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of having this example, we need unit tests verifying the pattern

@spokeydokeys

Copy link
Copy Markdown
Contributor Author

I removed the example and added a unit test to test_agent_runner.py.

There were a few linting and formatting issues that make lint and make check-format brought up that weren't in my changes, but I let ruff fix them and included them in the last commit. Happy to drop those if you'd rather leave them.

@spokeydokeys
spokeydokeys requested a review from seratch August 26, 2025 23:08
Comment thread src/agents/realtime/model_events.py Outdated

type: Literal["input_audio_transcription_completed"] = "input_audio_transcription_completed"


Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you remove these unrelated changes to make the review easier?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you remove these unrelated changes to make the review easier?

Of course! I removed the final commit that had all the make lint/check-format changes that weren't part of the bug fix.

@spokeydokeys
spokeydokeys requested a review from seratch August 29, 2025 15:18
@spokeydokeys

Copy link
Copy Markdown
Contributor Author

@seratch, I made the changes you were looking for. Would you like me to rebase or update the branch before you review?

@seratch
seratch merged commit 9ad2949 into openai:main Sep 9, 2025
5 checks passed
seratch pushed a commit that referenced this pull request Sep 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working feature:sessions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Session objects not updated until final output

2 participants