@@ -93,6 +93,34 @@ def _deserialize_response_format(response_format: Any) -> type[BaseModel] | None
9393 return None
9494
9595
96+ def _serialize_options (options : dict [str , Any ]) -> dict [str , Any ]:
97+ """Serialize known durable options without mutating caller-provided options."""
98+ result = dict (options )
99+ response_format = result .get ("response_format" )
100+ if (
101+ _PydanticBaseModel is not None
102+ and inspect .isclass (response_format )
103+ and issubclass (response_format , _PydanticBaseModel )
104+ ):
105+ result ["response_format" ] = serialize_response_format (response_format )
106+ return result
107+
108+
109+ def _deserialize_options (options : dict [str , Any ]) -> dict [str , Any ]:
110+ """Deserialize known durable options without mutating caller-provided options."""
111+ result = dict (options )
112+ if (response_format_value := result .get ("response_format" )) is not None :
113+ response_format = _deserialize_response_format (response_format_value )
114+ if response_format is not None :
115+ result ["response_format" ] = response_format
116+ elif (
117+ isinstance (response_format_value , dict )
118+ and response_format_value .get ("__response_schema_type__" ) == "pydantic_model"
119+ ):
120+ result .pop ("response_format" )
121+ return result
122+
123+
96124@dataclass
97125class RunRequest :
98126 """Represents a request to run an agent with a specific message and configuration.
@@ -165,7 +193,7 @@ def to_dict(self) -> dict[str, Any]:
165193 "role" : self .role ,
166194 "request_response_format" : self .request_response_format ,
167195 "correlationId" : self .correlation_id ,
168- "options" : self .options ,
196+ "options" : _serialize_options ( self .options ) ,
169197 }
170198 if self .response_format :
171199 result ["response_format" ] = serialize_response_format (self .response_format )
@@ -200,6 +228,7 @@ def from_dict(cls, data: dict[str, Any]) -> RunRequest:
200228 raise ValueError ("correlationId is required in RunRequest data" )
201229
202230 options = data .get ("options" )
231+ options = _deserialize_options (options ) if isinstance (options , dict ) else {}
203232
204233 return cls (
205234 message = data .get ("message" , "" ),
@@ -211,7 +240,7 @@ def from_dict(cls, data: dict[str, Any]) -> RunRequest:
211240 enable_tool_calls = data .get ("enable_tool_calls" , True ),
212241 created_at = created_at ,
213242 orchestration_id = data .get ("orchestrationId" ),
214- options = cast ( dict [ str , Any ], options ) if isinstance ( options , dict ) else {} ,
243+ options = options ,
215244 )
216245
217246
0 commit comments