[BREAKING] Python: add Responses conversation ID helper (#7234)

* Python: add Responses conversation ID helper

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 2dd9980a-b869-4c16-8642-75b7a6d6ebdf

* Python: make Responses session flag optional

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 2dd9980a-b869-4c16-8642-75b7a6d6ebdf

* Python: correlate Responses session return types

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 2dd9980a-b869-4c16-8642-75b7a6d6ebdf

* Python: clarify Responses conversation parameter

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 2dd9980a-b869-4c16-8642-75b7a6d6ebdf

* Python: clarify streaming conversation parameter

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 2dd9980a-b869-4c16-8642-75b7a6d6ebdf

* Python: include conversation in created event

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 2dd9980a-b869-4c16-8642-75b7a6d6ebdf

* Python: warn on nonstandard Responses IDs

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 2dd9980a-b869-4c16-8642-75b7a6d6ebdf
This commit is contained in:
Eduard van Valkenburg
2026-07-21 17:17:14 +02:00
committed by GitHub
parent f6a3c43e9a
commit a70fe21298
7 changed files with 107 additions and 54 deletions
@@ -117,8 +117,8 @@ async def responses(body: dict[str, Any] = Body(...)) -> JSONResponse | Streamin
run = responses_to_run(body)
except ValueError as exc:
raise HTTPException(status_code=400, detail=str(exc)) from exc
session_id = responses_session_id(body)
conversation_id = session_id if body.get("conversation_id") == session_id else None
session_id, is_conversation_id = responses_session_id(body)
conversation_id = session_id if is_conversation_id else None
response_id = create_response_id()
# App-specific policy: allow only the request options this route is willing
@@ -148,7 +148,7 @@ async def responses(body: dict[str, Any] = Body(...)) -> JSONResponse | Streamin
async for event in responses_from_streaming_run(
stream,
response_id=response_id,
session_id=session_id,
conversation_id=conversation_id,
):
yield event
# `agent.run(..., stream=True)` updates the session while the stream
@@ -184,7 +184,7 @@ async def responses(body: dict[str, Any] = Body(...)) -> JSONResponse | Streamin
responses_from_run(
result,
response_id=response_id,
session_id=session_id,
conversation_id=conversation_id,
)
)
@@ -230,10 +230,10 @@ async def responses(body: dict[str, Any] = Body(...)) -> JSONResponse: # noqa:
raise HTTPException(status_code=400, detail=str(exc)) from exc
# This sample demonstrates only Responses `previous_response_id`
# continuation. `responses_session_id` also returns `conversation_id`, so
# reject that shape here instead of treating it as a checkpoint cursor.
previous_response_id = responses_session_id(body)
if previous_response_id and not previous_response_id.startswith("resp_"):
# continuation, so reject `conversation_id` instead of treating it as a
# checkpoint cursor.
previous_response_id, is_conversation_id = responses_session_id(body)
if is_conversation_id:
raise HTTPException(
status_code=400,
detail="This server supports previous_response_id continuation only; conversation_id is not implemented.",
@@ -267,7 +267,6 @@ async def responses(body: dict[str, Any] = Body(...)) -> JSONResponse: # noqa:
responses_from_run(
response_from_workflow_result(result),
response_id=response_id,
session_id=previous_response_id,
)
)