Skip to content

Commit 91d7951

Browse files
Preserve raw tool-call JSON for deterministic local inference
1 parent 843ac58 commit 91d7951

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

mini_agent/llm/openai_client.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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
)

mini_agent/schema/schema.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ class FunctionCall(BaseModel):
1515
"""Function call details."""
1616

1717
name: str
18-
arguments: dict[str, Any] # Function arguments as dict
18+
arguments: dict[str, Any] # Parsed function arguments
19+
arguments_json: str | None = None # Raw JSON string (for deterministic replay)
1920

2021

2122
class ToolCall(BaseModel):

0 commit comments

Comments
 (0)