normalizeToolCallInput guarantees tool call input is a JSON object string. Anthropic drops assistant tool calls with malformed input, which can leave following tool results orphaned.
(input string)
| 617 | // Anthropic drops assistant tool calls with malformed input, which can leave |
| 618 | // following tool results orphaned. |
| 619 | func normalizeToolCallInput(input string) string { |
| 620 | input = strings.TrimSpace(input) |
| 621 | if input == "" { |
| 622 | return "{}" |
| 623 | } |
| 624 | |
| 625 | var object map[string]any |
| 626 | if err := json.Unmarshal([]byte(input), &object); err != nil || object == nil { |
| 627 | return "{}" |
| 628 | } |
| 629 | |
| 630 | return input |
| 631 | } |
| 632 | |
| 633 | // ExtractToolCalls returns all tool call parts as content blocks. |
| 634 | func ExtractToolCalls(parts []fantasy.MessagePart) []fantasy.ToolCallContent { |
no test coverage detected