Implementation:Openai Openai agents python Previous Response Id Pattern
| Knowledge Sources | |
|---|---|
| Domains | Conversational AI, Agent Orchestration, Stateful Conversations |
| Last Updated | 2026-02-11 00:00 GMT |
Overview
Demonstrates conversation continuation using the previous_response_id parameter to allow the model to retain context across multiple Runner.run() and Runner.run_streamed() calls without re-sending prior messages.
Description
The previous_response_id pattern enables stateful multi-turn conversations by passing the response ID from a prior run into a subsequent run. When the OpenAI Responses API receives this parameter, it automatically retrieves the conversation history associated with that response, allowing the model to continue the conversation without the caller needing to manually manage and re-send previous messages.
The example creates a simple assistant agent and asks it a geography question ("What is the largest country in South America?"). The first call returns both the answer and a response ID. The second call then asks a follow-up question ("What is the capital of that country?") and passes previous_response_id=result.last_response_id, allowing the model to correctly resolve the anaphoric reference "that country" to Brazil from the prior turn.
Both synchronous (Runner.run()) and streaming (Runner.run_streamed()) modes are demonstrated. This pattern only applies to the OpenAI Responses API; other model providers will ignore this parameter. Responses are stored for 30 days, so production implementations should track expiration dates and fall back to re-sending full conversation history when a response ID has expired.
Usage
Use this pattern when building multi-turn conversational agents that need to maintain context across interactions without the overhead of managing and transmitting the full conversation history on each request. It is particularly useful for chatbot interfaces, interactive assistants, and any scenario where sequential queries build upon prior answers.
Code Reference
Source Location
- Repository: Openai_Openai_agents_python
- File: examples/basic/previous_response_id.py
- Lines: 1-67
Signature
result = await Runner.run(
agent,
"What is the capital of that country?",
previous_response_id=result.last_response_id,
)
Import
from agents import Agent, Runner
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| agent | Agent |
Yes | The agent instance to run the conversation with |
| input | str |
Yes | The user message or query for the current turn |
| previous_response_id | None | No | The response ID from a prior run, enabling the model to access previous conversation context |
Outputs
| Name | Type | Description |
|---|---|---|
| result.final_output | str |
The agent's text response to the current input |
| result.last_response_id | str |
The response ID for the current turn, which can be passed to subsequent runs |
Usage Examples
Synchronous Multi-Turn Conversation
import asyncio
from agents import Agent, Runner
async def main():
agent = Agent(
name="Assistant",
instructions="You are a helpful assistant. be VERY concise.",
)
result = await Runner.run(agent, "What is the largest country in South America?")
print(result.final_output)
# Brazil
result = await Runner.run(
agent,
"What is the capital of that country?",
previous_response_id=result.last_response_id,
)
print(result.final_output)
# Brasilia
asyncio.run(main())
Streaming Multi-Turn Conversation
import asyncio
from agents import Agent, Runner
async def main_stream():
agent = Agent(
name="Assistant",
instructions="You are a helpful assistant. be VERY concise.",
)
result = Runner.run_streamed(agent, "What is the largest country in South America?")
async for event in result.stream_events():
if event.type == "raw_response_event" and event.data.type == "response.output_text.delta":
print(event.data.delta, end="", flush=True)
print()
result = Runner.run_streamed(
agent,
"What is the capital of that country?",
previous_response_id=result.last_response_id,
)
async for event in result.stream_events():
if event.type == "raw_response_event" and event.data.type == "response.output_text.delta":
print(event.data.delta, end="", flush=True)
asyncio.run(main_stream())