@@ -144,13 +144,19 @@ def _convert_messages(self, messages: list[Message]) -> tuple[str | None, list[d
144144 if msg .tool_calls :
145145 tool_calls_list = []
146146 for tool_call in msg .tool_calls :
147+ arguments_json = tool_call .function .arguments_json
148+ if arguments_json is None :
149+ # Fall back to deterministic dump if raw string missing
150+ arguments_json = json .dumps (
151+ tool_call .function .arguments , separators = ("," , ":" ), sort_keys = True
152+ )
147153 tool_calls_list .append (
148154 {
149155 "id" : tool_call .id ,
150156 "type" : "function" ,
151157 "function" : {
152158 "name" : tool_call .function .name ,
153- "arguments" : json . dumps ( tool_call . function . arguments ) ,
159+ "arguments" : arguments_json ,
154160 },
155161 }
156162 )
@@ -223,8 +229,9 @@ def _parse_response(self, response: Any) -> LLMResponse:
223229 tool_calls = []
224230 if response .tool_calls :
225231 for tool_call in response .tool_calls :
226- # Parse arguments from JSON string
227- arguments = json .loads (tool_call .function .arguments )
232+ # Parse arguments from JSON string while preserving the raw text
233+ raw_arguments = tool_call .function .arguments
234+ arguments = json .loads (raw_arguments ) if raw_arguments else {}
228235
229236 tool_calls .append (
230237 ToolCall (
@@ -233,6 +240,7 @@ def _parse_response(self, response: Any) -> LLMResponse:
233240 function = FunctionCall (
234241 name = tool_call .function .name ,
235242 arguments = arguments ,
243+ arguments_json = raw_arguments ,
236244 ),
237245 )
238246 )
0 commit comments