@@ -522,14 +522,19 @@ func buildAnthropicMessageContent(msg core.Message) (any, error) {
522522 })
523523 }
524524 for _ , toolCall := range msg .ToolCalls {
525+ toolCallID := providers .ResponsesFunctionCallCallID (strings .TrimSpace (toolCall .ID ))
526+ toolName := strings .TrimSpace (toolCall .Function .Name )
527+ if toolName == "" {
528+ return nil , core .NewInvalidRequestError ("tool_call.function.name is required" , nil )
529+ }
525530 input , err := parseToolCallArguments (toolCall .Function .Arguments )
526531 if err != nil {
527532 return nil , err
528533 }
529534 blocks = append (blocks , anthropicMessageContentBlock {
530535 Type : "tool_use" ,
531- ID : toolCall . ID ,
532- Name : toolCall . Function . Name ,
536+ ID : toolCallID ,
537+ Name : toolName ,
533538 Input : input ,
534539 })
535540 }
@@ -870,7 +875,10 @@ func (sc *streamConverter) convertEvent(event *anthropicStreamEvent) string {
870875
871876 initialArguments := extractInitialToolArguments (event .ContentBlock .Input )
872877 state .PlaceholderObject = initialArguments == "{}"
873- if initialArguments != "" {
878+ emittedArguments := initialArguments
879+ if state .PlaceholderObject {
880+ emittedArguments = ""
881+ } else if initialArguments != "" {
874882 _ , _ = state .Arguments .WriteString (initialArguments )
875883 }
876884 sc .toolCalls [event .Index ] = state
@@ -883,7 +891,7 @@ func (sc *streamConverter) convertEvent(event *anthropicStreamEvent) string {
883891 "type" : "function" ,
884892 "function" : map [string ]any {
885893 "name" : state .Name ,
886- "arguments" : initialArguments ,
894+ "arguments" : emittedArguments ,
887895 },
888896 },
889897 },
@@ -1750,21 +1758,32 @@ func (sc *responsesStreamConverter) newResponsesToolCallState(contentBlock *anth
17501758
17511759 initialArguments := extractInitialToolArguments (contentBlock .Input )
17521760 state .PlaceholderObject = initialArguments == "{}"
1753- if initialArguments != "" {
1761+ if initialArguments != "" && ! state . PlaceholderObject {
17541762 _ , _ = state .Arguments .WriteString (initialArguments )
17551763 }
17561764
17571765 return state
17581766}
17591767
1760- func (sc * responsesStreamConverter ) renderResponsesToolCallItem (state * responsesToolCallState , status string ) map [string ]any {
1768+ func (sc * responsesStreamConverter ) toolCallArguments (state * responsesToolCallState ) string {
1769+ if state .PlaceholderObject && state .Arguments .Len () == 0 {
1770+ return "{}"
1771+ }
1772+ return state .Arguments .String ()
1773+ }
1774+
1775+ func (sc * responsesStreamConverter ) renderResponsesToolCallItem (state * responsesToolCallState , status string , includePlaceholder bool ) map [string ]any {
1776+ arguments := state .Arguments .String ()
1777+ if includePlaceholder {
1778+ arguments = sc .toolCallArguments (state )
1779+ }
17611780 item := map [string ]any {
17621781 "id" : state .ID ,
17631782 "type" : "function_call" ,
17641783 "status" : status ,
17651784 "call_id" : state .CallID ,
17661785 "name" : state .Name ,
1767- "arguments" : state . Arguments . String () ,
1786+ "arguments" : arguments ,
17681787 }
17691788 return item
17701789}
@@ -1797,7 +1816,7 @@ func (sc *responsesStreamConverter) convertEvent(event *anthropicStreamEvent) st
17971816 sc .toolCalls [event .Index ] = state
17981817 return sc .writeResponsesEvent ("response.output_item.added" , map [string ]any {
17991818 "type" : "response.output_item.added" ,
1800- "item" : sc .renderResponsesToolCallItem (state , "in_progress" ),
1819+ "item" : sc .renderResponsesToolCallItem (state , "in_progress" , false ),
18011820 "output_index" : state .OutputIndex ,
18021821 })
18031822 }
@@ -1855,10 +1874,10 @@ func (sc *responsesStreamConverter) convertEvent(event *anthropicStreamEvent) st
18551874 "type" : "response.function_call_arguments.done" ,
18561875 "item_id" : state .ID ,
18571876 "output_index" : state .OutputIndex ,
1858- "arguments" : state . Arguments . String ( ),
1877+ "arguments" : sc . toolCallArguments ( state ),
18591878 }) + sc .writeResponsesEvent ("response.output_item.done" , map [string ]any {
18601879 "type" : "response.output_item.done" ,
1861- "item" : sc .renderResponsesToolCallItem (state , "completed" ),
1880+ "item" : sc .renderResponsesToolCallItem (state , "completed" , true ),
18621881 "output_index" : state .OutputIndex ,
18631882 })
18641883
0 commit comments