Skip to content

Commit e8c9873

Browse files
Ensure tool-call JSON stays deterministic for local models
1 parent 843ac58 commit e8c9873

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

mini_agent/llm/openai_client.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,12 @@ async def _make_api_request(
6565
params = {
6666
"model": self.model,
6767
"messages": api_messages,
68+
# Add consistent parameters for better KV cache stability
69+
"temperature": 0.7, # Consistent temperature
70+
"seed": 42, # Fixed seed for deterministic behavior and better caching
6871
# Enable reasoning_split to separate thinking content
69-
"extra_body": {"reasoning_split": True},
72+
# NOTE: Commenting out for local models - may cause cache invalidation
73+
# "extra_body": {"reasoning_split": True},
7074
}
7175

7276
if tools:
@@ -144,13 +148,19 @@ def _convert_messages(self, messages: list[Message]) -> tuple[str | None, list[d
144148
if msg.tool_calls:
145149
tool_calls_list = []
146150
for tool_call in msg.tool_calls:
151+
arguments_json = tool_call.function.arguments_json
152+
if arguments_json is None:
153+
# Fall back to deterministic dump if raw string missing
154+
arguments_json = json.dumps(
155+
tool_call.function.arguments, separators=(",", ":"), sort_keys=True
156+
)
147157
tool_calls_list.append(
148158
{
149159
"id": tool_call.id,
150160
"type": "function",
151161
"function": {
152162
"name": tool_call.function.name,
153-
"arguments": json.dumps(tool_call.function.arguments),
163+
"arguments": arguments_json,
154164
},
155165
}
156166
)
@@ -223,8 +233,9 @@ def _parse_response(self, response: Any) -> LLMResponse:
223233
tool_calls = []
224234
if response.tool_calls:
225235
for tool_call in response.tool_calls:
226-
# Parse arguments from JSON string
227-
arguments = json.loads(tool_call.function.arguments)
236+
# Parse arguments from JSON string while preserving the raw text
237+
raw_arguments = tool_call.function.arguments
238+
arguments = json.loads(raw_arguments) if raw_arguments else {}
228239

229240
tool_calls.append(
230241
ToolCall(
@@ -233,6 +244,7 @@ def _parse_response(self, response: Any) -> LLMResponse:
233244
function=FunctionCall(
234245
name=tool_call.function.name,
235246
arguments=arguments,
247+
arguments_json=raw_arguments,
236248
),
237249
)
238250
)

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)