-
-
Notifications
You must be signed in to change notification settings - Fork 15k
[responsesAPI][bugfix] serialize harmony messages #26185
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
Changes from all commits
9164e52
e955754
b3b9249
230242c
cc723bd
b2980a8
2a05647
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 |
|---|---|---|
|
|
@@ -63,6 +63,7 @@ | |
| Field, | ||
| TypeAdapter, | ||
| ValidationInfo, | ||
| field_serializer, | ||
| field_validator, | ||
| model_validator, | ||
| ) | ||
|
|
@@ -2078,11 +2079,6 @@ class ResponsesResponse(OpenAIBaseModel): | |
| model: str | ||
| object: Literal["response"] = "response" | ||
| output: list[ResponseOutputItem] | ||
| # These are populated when enable_response_messages is set to True | ||
| # TODO: Currently an issue where content of harmony messages | ||
| # is not available when these are serialized. Metadata is available | ||
| input_messages: Optional[list[ChatCompletionMessageParam]] = None | ||
| output_messages: Optional[list[ChatCompletionMessageParam]] = None | ||
| parallel_tool_calls: bool | ||
| temperature: float | ||
| tool_choice: ToolChoice | ||
|
|
@@ -2102,6 +2098,49 @@ class ResponsesResponse(OpenAIBaseModel): | |
| usage: Optional[ResponseUsage] = None | ||
| user: Optional[str] = None | ||
|
|
||
| # --8<-- [start:responses-extra-params] | ||
| # These are populated when enable_response_messages is set to True | ||
| # NOTE: custom serialization is needed | ||
| # see serialize_input_messages and serialize_output_messages | ||
| input_messages: Optional[list[ChatCompletionMessageParam]] = None | ||
| output_messages: Optional[list[ChatCompletionMessageParam]] = None | ||
| # --8<-- [end:responses-extra-params] | ||
|
|
||
| # NOTE: openAI harmony doesn't serialize TextContent properly, | ||
| # TODO: this fixes for TextContent, but need to verify for tools etc | ||
| # https://github.com/openai/harmony/issues/78 | ||
| @field_serializer("output_messages", when_used="json") | ||
| def serialize_output_messages(self, msgs, _info): | ||
qandrew marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if msgs: | ||
| serialized = [] | ||
| for m in msgs: | ||
| if isinstance(m, dict): | ||
| serialized.append(m) | ||
| elif hasattr(m, "__dict__"): | ||
qandrew marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| serialized.append(m.to_dict()) | ||
| else: | ||
| # fallback to pyandic dump | ||
| serialized.append(m.model_dump_json()) | ||
| return serialized | ||
| return None | ||
|
|
||
| # NOTE: openAI harmony doesn't serialize TextContent properly, this fixes it | ||
| # https://github.com/openai/harmony/issues/78 | ||
| @field_serializer("input_messages", when_used="json") | ||
| def serialize_input_messages(self, msgs, _info): | ||
| if msgs: | ||
| serialized = [] | ||
| for m in msgs: | ||
| if isinstance(m, dict): | ||
| serialized.append(m) | ||
| elif hasattr(m, "__dict__"): | ||
| serialized.append(m.to_dict()) | ||
| else: | ||
| # fallback to pyandic dump | ||
| serialized.append(m.model_dump_json()) | ||
| return serialized | ||
|
Comment on lines
+2132
to
+2141
Collaborator
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. Seems we could consolidate majority of the code (e.g. message to serialized_message mapping) to a separate function? It could allow us to
Collaborator
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. Trying to refactor it a bit in #26620 |
||
| return None | ||
|
|
||
| @classmethod | ||
| def from_request( | ||
| cls, | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.