-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Fix responses API #8880
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
Fix responses API #8880
Changes from all commits
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 |
|---|---|---|
|
|
@@ -217,26 +217,43 @@ def _extract_citations_from_response(self, choice): | |
|
|
||
| def _process_response(self, response): | ||
| """Process the response of OpenAI Response API and extract outputs. | ||
|
|
||
| Args: | ||
| response: OpenAI Response API response | ||
| https://platform.openai.com/docs/api-reference/responses/object | ||
|
|
||
| Returns: | ||
| List of processed outputs | ||
| List of processed outputs, which is always of size 1 because the Response API only supports one output. | ||
| """ | ||
| outputs = [] | ||
| text_outputs = [] | ||
| tool_calls = [] | ||
| reasoning_contents = [] | ||
|
|
||
| for output_item in response.output: | ||
| if output_item.type == "message": | ||
| output_item_type = output_item.type | ||
| if output_item_type == "message": | ||
| for content_item in output_item.content: | ||
| outputs.append(content_item.text) | ||
| elif output_item.type == "function_call": | ||
| text_outputs.append(content_item.text) | ||
| elif output_item_type == "function_call": | ||
| tool_calls.append(output_item.model_dump()) | ||
| elif output_item_type == "reasoning": | ||
| if getattr(output_item, "content", None) and len(output_item.content) > 0: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. q: is it possible that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. technically it's possible that either content or summary is unset, but again OpenAI is doing an odd job documenting Responses API. |
||
| for content_item in output_item.content: | ||
| reasoning_contents.append(content_item.text) | ||
| elif getattr(output_item, "summary", None) and len(output_item.summary) > 0: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||
| for summary_item in output_item.summary: | ||
| reasoning_contents.append(summary_item.text) | ||
|
|
||
| result = {} | ||
| if len(text_outputs) > 0: | ||
| result["text"] = "".join(text_outputs) | ||
| if len(tool_calls) > 0: | ||
| result["tool_calls"] = tool_calls | ||
| if len(reasoning_contents) > 0: | ||
| result["reasoning_content"] = "".join(reasoning_contents) | ||
| # All `response.output` items map to one answer, so we return a list of size 1. | ||
| return [result] | ||
|
|
||
| if tool_calls: | ||
| outputs.append({"tool_calls": tool_calls}) | ||
| return outputs | ||
|
|
||
|
|
||
| def inspect_history(n: int = 1): | ||
|
|
||
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.
nit: Maybe add a comment that even though the output field of the responses API is a list, all output items belong to a single choice
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.
done!