@@ -261,16 +261,33 @@ async def test_generate_response_with_tool_calls(
261261
262262
263263@pytest .mark .asyncio
264- async def test_generate_response_tool_call_json_error (
264+ async def test_generate_response_tool_call_json_repair (
265265 mock_async_openai ,
266266 minimal_config ,
267267 mock_tool_call_impl ,
268268):
269- """Test handling of invalid JSON in tool call arguments."""
269+ """Test that json-repair successfully fixes invalid JSON in tool call arguments.
270+
271+ This test uses a MemMachine memory search example with multiple JSON errors
272+ that commonly occur in LLM outputs to verify json-repair functionality.
273+ """
270274 mock_tool_call = mock_tool_call_impl ()
271- mock_tool_call .uuid = "call_123"
272- mock_tool_call .function .name = "get_weather"
273- mock_tool_call .function .arguments = '{"location": "Boston",}'
275+ mock_tool_call .id = "call_123" # Use id instead of uuid
276+ mock_tool_call .function .name = "search_memory"
277+ # Invalid JSON with multiple common LLM errors:
278+ # - Missing quotes around keys
279+ # - Trailing comma in array
280+ # - Comments in JSON (not standard)
281+ # - Missing closing brace (will be fixed)
282+ mock_tool_call .function .arguments = """{
283+ query: "user preferences and history",
284+ "top_k": 10,
285+ "types": ["episodic", "semantic",],
286+ "filter": {
287+ user_id: "user_123",
288+ "session_active": true,
289+ },
290+ }"""
274291
275292 mock_response = MagicMock ()
276293 mock_response .choices [0 ].message .tool_calls = [mock_tool_call ]
@@ -280,8 +297,20 @@ async def test_generate_response_tool_call_json_error(
280297 mock_client .chat .completions .create .return_value = mock_response
281298
282299 lm = OpenAIChatCompletionsLanguageModel (minimal_config )
283- with pytest .raises (ValueError , match = "JSON decode error" ):
284- await lm .generate_response ()
300+ # json-repair should automatically fix the invalid JSON
301+ _content , tool_calls = await lm .generate_response ()
302+
303+ # Verify the repaired JSON is parsed correctly
304+ assert len (tool_calls ) == 1
305+ assert tool_calls [0 ]["call_id" ] == "call_123"
306+ assert tool_calls [0 ]["function" ]["name" ] == "search_memory"
307+ # Verify the complex JSON structure was repaired and parsed correctly
308+ args = tool_calls [0 ]["function" ]["arguments" ]
309+ assert args ["query" ] == "user preferences and history"
310+ assert args ["top_k" ] == 10
311+ assert args ["types" ] == ["episodic" , "semantic" ]
312+ assert args ["filter" ]["user_id" ] == "user_123"
313+ assert args ["filter" ]["session_active" ] is True
285314
286315
287316@pytest .mark .asyncio
0 commit comments