feat: handle incorrect booleans in json from llm (#2820)

This commit is contained in:
cthomas
2025-09-10 12:31:40 -07:00
committed by GitHub
parent e18ff39a76
commit ea10f55097
2 changed files with 12 additions and 6 deletions

View File

@@ -106,15 +106,19 @@ class AnthropicStreamingInterface:
try:
tool_input = json.loads(self.accumulated_tool_call_args)
except json.JSONDecodeError as e:
logger.warning(
f"Failed to decode tool call arguments for tool_call_id={self.tool_call_id}, "
f"name={self.tool_call_name}. Raw input: {self.accumulated_tool_call_args!r}. Error: {e}"
)
raise
# Attempt to use OptimisticJSONParser to handle incomplete/malformed JSON
try:
tool_input = self.json_parser.parse(self.accumulated_tool_call_args)
except:
logger.warning(
f"Failed to decode tool call arguments for tool_call_id={self.tool_call_id}, "
f"name={self.tool_call_name}. Raw input: {self.accumulated_tool_call_args!r}. Error: {e}"
)
raise e
if "id" in tool_input and tool_input["id"].startswith("toolu_") and "function" in tool_input:
arguments = str(json.dumps(tool_input["function"]["arguments"], indent=2))
else:
arguments = self.accumulated_tool_call_args
arguments = str(json.dumps(tool_input, indent=2))
return ToolCall(id=self.tool_call_id, function=FunctionCall(arguments=arguments, name=self.tool_call_name))
def _check_inner_thoughts_complete(self, combined_args: str) -> bool:

View File

@@ -63,6 +63,8 @@ class OptimisticJSONParser(JSONParser):
'"': self._parse_string,
"t": self._parse_true,
"f": self._parse_false,
"T": self._parse_true,
"F": self._parse_false,
"n": self._parse_null,
}
# Register number parser for digits and signs