How to beginner · 3 min read

Responses API previous_response_id explained

Quick answer
The previous_response_id parameter in the OpenAI Responses API lets you reference a prior completion's ID to create a linked chain of responses, enabling context continuity or incremental generation. Use it to build conversational threads or track response lineage in your application.

PREREQUISITES

  • Python 3.8+
  • OpenAI API key (free tier works)
  • pip install openai>=1.0

Setup

Install the official OpenAI Python SDK and set your API key as an environment variable to authenticate requests.

bash
pip install openai>=1.0

Step by step

This example demonstrates how to create a chat completion and then use its id as the previous_response_id in a subsequent request to maintain response linkage.

python
import os
from openai import OpenAI

client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

# First completion
response1 = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "Hello, who won the 2024 Olympics?"}]
)
print("First response:", response1.choices[0].message.content)

# Use the previous response ID to chain
previous_id = response1.id

response2 = client.chat.completions.create(
    model="gpt-4o",
    previous_response_id=previous_id,
    messages=[{"role": "user", "content": "Can you give more details about the gold medalists?"}]
)
print("Second response:", response2.choices[0].message.content)
output
First response: The 2024 Olympics were held in Paris, and the United States topped the medal count.
Second response: The U.S. won gold medals in swimming, athletics, and gymnastics, with standout athletes like Katie Ledecky and Caeleb Dressel making history.

Common variations

  • Use previous_response_id to build multi-turn conversations or incremental content generation.
  • Combine with different models like gpt-4o-mini for cost-effective chaining.
  • Use async calls with asyncio for concurrent chained requests.
python
import asyncio
from openai import OpenAI

async def main():
    client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

    response1 = await client.chat.completions.acreate(
        model="gpt-4o-mini",
        messages=[{"role": "user", "content": "Summarize the latest AI trends."}]
    )
    print("Async first response:", response1.choices[0].message.content)

    response2 = await client.chat.completions.acreate(
        model="gpt-4o-mini",
        previous_response_id=response1.id,
        messages=[{"role": "user", "content": "Expand on AI in healthcare."}]
    )
    print("Async second response:", response2.choices[0].message.content)

asyncio.run(main())
output
Async first response: AI trends include generative models, multimodal AI, and increased adoption in industries.
Async second response: In healthcare, AI is improving diagnostics, personalized medicine, and drug discovery.

Troubleshooting

  • If you get an error about previous_response_id not found, verify the ID is correct and from the same model and API version.
  • Ensure your API key has permissions for the Responses API features.
  • Check that the previous_response_id is used only in supported endpoints; it is not valid for all request types.

Key Takeaways

  • Use previous_response_id to link completions and maintain context across calls.
  • Always retrieve and store the id from the initial response to pass in subsequent requests.
  • The parameter enables building conversational threads or incremental content generation efficiently.
Verified 2026-04 · gpt-4o, gpt-4o-mini
Verify ↗