-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Fixes #3008 Responses API: multi-turn conversations 400 on turn 2 when passing response.output back as input #3053
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
0faec06
57d56ff
cbe9810
b5de720
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| # File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. | ||
|
|
||
| from typing import List, Union, Optional | ||
| from typing import Any, Dict, List, Union, Optional | ||
| from typing_extensions import Literal, TypeAlias | ||
|
|
||
| from .tool import Tool | ||
|
|
@@ -319,3 +319,31 @@ def output_text(self) -> str: | |
| texts.append(content.text) | ||
|
|
||
| return "".join(texts) | ||
|
|
||
| def output_as_input(self) -> List[Dict[str, Any]]: | ||
| """Return the response output items as a list of input-ready dicts for use in a subsequent turn. | ||
|
|
||
| This is the recommended way to build multi-turn conversations manually. | ||
| It calls ``as_input()`` on items that have that method (e.g. | ||
| :class:`ResponseOutputMessage`, :class:`ResponseReasoningItem`, | ||
| :class:`ResponseFunctionToolCall`), stripping output-only fields like | ||
| ``status`` and ``encrypted_content`` that the API rejects as input. | ||
| For all other output item types, ``to_dict()`` is used as a fallback. | ||
|
|
||
| Example:: | ||
|
|
||
| conversation: list[dict] = [] | ||
| for msg in ["What is the capital of France?", "And Germany?"]: | ||
| conversation.append({"role": "user", "content": msg}) | ||
| response = client.responses.create( | ||
| model="o4-mini", input=conversation, max_output_tokens=200, | ||
| ) | ||
| conversation.extend(response.output_as_input()) | ||
| """ | ||
| result: List[Dict[str, Any]] = [] | ||
| for item in self.output: | ||
| if hasattr(item, "as_input"): | ||
| result.append(item.as_input()) | ||
| else: | ||
| result.append(item.to_dict()) | ||
|
||
| return result | ||
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.
The new fallback path serializes unhandled output variants with
item.to_dict()unchanged, which can still emit output-only keys and trigger the same 400 errors this change is trying to prevent. For example,ResponseToolSearchCallincludescreated_by(response_tool_search_call.py:30), but the corresponding input shapeToolSearchCalldoes not accept that field (response_input_item_param.py:153-170), soresponse.output_as_input()can produce invalid follow-up input whenever such items appear inresponse.output.Useful? React with 👍 / 👎.
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.
@chatgpt-codex-connector
pls check again, I fixed