[ 
https://issues.apache.org/jira/browse/CAMEL-23956?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Federico Mariani updated CAMEL-23956:
-------------------------------------
    Attachment: OpenAIAgenticLoopMalformedToolArgumentsTest.java

> camel-openai: malformed tool-call arguments crash the agentic loop instead of 
> being returned to the model
> ---------------------------------------------------------------------------------------------------------
>
>                 Key: CAMEL-23956
>                 URL: https://issues.apache.org/jira/browse/CAMEL-23956
>             Project: Camel
>          Issue Type: Bug
>          Components: camel-openai
>    Affects Versions: 4.21.0
>            Reporter: Federico Mariani
>            Priority: Major
>              Labels: ai
>         Attachments: OpenAIAgenticLoopMalformedToolArgumentsTest.java
>
>
> Models occasionally emit syntactically invalid JSON in 
> {{tool_calls[].function.arguments}} — a hallucination class every agentic 
> runtime must handle. The documented error contract of the agentic loop 
> ({{openai-mcp.adoc}}, "Error Handling in the Agentic Loop") is that tool 
> execution problems are caught, logged and sent back to the model as tool 
> result text so it can recover.
> However, in {{OpenAIProducer.processNonStreamingAgentic}} the argument 
> parsing happens *outside* the try/catch that implements that contract:
> {code:java}
> Map<String, Object> argsMap = OBJECT_MAPPER.readValue(argsJson, Map.class); 
> // line ~523, not guarded
> String resultContent;
> try {
>     McpSchema.CallToolResult toolResult = getEndpoint().callTool(mcpClient, 
> toolName, argsMap);
>     ...
> } catch (Exception e) {
>     resultContent = "Error: Tool execution failed: " + e.getMessage();
> }
> {code}
> A malformed arguments string therefore fails the whole exchange with a raw 
> Jackson exception:
> {noformat}
> com.fasterxml.jackson.core.io.JsonEOFException: Unexpected end-of-input: 
> expected close marker for Object
> {noformat}
> {{OpenAIToolExecutionProducer.process}} (line ~145) has the identical 
> unguarded {{readValue}}.
> h3. Suggested fix
> Move the parse inside the guarded section (or its own try/catch) and feed a 
> corrective tool result back to the model, e.g. {{"Error: invalid tool 
> arguments: " + e.getMessage()}} — mirroring how MCP execution failures are 
> already handled. The attached route-based reproducer 
> ({{OpenAIAgenticLoopMalformedToolArgumentsTest}}, using 
> {{camel-test-infra-openai-mock}}) asserts the recovery behavior and fails on 
> current main.
> Related: CAMEL-23943 documents the same defect family in 
> camel-langchain4j-tools. The fix should stay compatible with the error-policy 
> model of the unified AI tool abstraction (CAMEL-23382, 
> {{AiToolResult.ArgumentError}}), which camel-openai will adopt in roadmap 
> step 6.
> _This issue was drafted by Claude Code on behalf of Federico Mariani_



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to