Skip to content

Commit dbc6023

Browse files
authored
Build streamed OpenAI events before serializing them (#1586)
* Fall back to model_dump_json for OpenAI payload serialization OpenAI response and stream event types whose pydantic serializer is a lazily-built MockValSer cannot be serialized by the generic any-schema serializer, raising PydanticSerializationError (e.g. when streaming via WorkflowStreamClient). The model's own model_dump_json() handles them. Fixes #1585 * Dispatch pydantic models to their own serializer OpenAI's BaseModel sets defer_build=True, so a model's serializer is a MockValSer placeholder until pydantic's lazy build runs. The generic any-schema serializer reaches for that placeholder directly without triggering the build and raises PydanticSerializationError. Route pydantic models through their own model_dump_json (which triggers the build) by type instead of catching the error; non-model values continue through the generic serializer unchanged. * Build streamed events at the source instead of in the converter Force the deferred pydantic build on each streamed event before it is published or returned, so it serializes regardless of build state. This also covers the activity's list return value, which the payload converter serializes generically and cannot build on its own. Drop the now-redundant to_payload override.
1 parent 8d66724 commit dbc6023

1 file changed

Lines changed: 3 additions & 0 deletions

File tree

temporalio/contrib/openai_agents/_invoke_model_activity.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,9 @@ async def invoke_model_activity_streaming(
393393
conversation_id=input.get("conversation_id"),
394394
prompt=input.get("prompt"),
395395
):
396+
# OpenAI models set defer_build=True, so an event's pydantic
397+
# schema may still be an unbuilt placeholder.
398+
type(event).model_rebuild()
396399
events.append(event)
397400
events_topic.publish(event)
398401
except APIStatusError as e:

0 commit comments

Comments
 (0)