diff --git a/core/http/endpoints/anthropic/messages.go b/core/http/endpoints/anthropic/messages.go index 7736e70b2da0..8c5bed703ee0 100644 --- a/core/http/endpoints/anthropic/messages.go +++ b/core/http/endpoints/anthropic/messages.go @@ -357,7 +357,7 @@ func handleAnthropicStream(c echo.Context, id string, input *schema.AnthropicReq // Send initial content_block_start event contentBlockStart := schema.AnthropicStreamEvent{ Type: "content_block_start", - Index: currentBlockIndex, + Index: intPtr(currentBlockIndex), ContentBlock: &schema.AnthropicContentBlock{Type: "text", Text: ""}, } sendAnthropicSSE(c, contentBlockStart) @@ -376,7 +376,7 @@ func handleAnthropicStream(c echo.Context, id string, input *schema.AnthropicReq if !inToolCall && currentBlockIndex == 0 { sendAnthropicSSE(c, schema.AnthropicStreamEvent{ Type: "content_block_stop", - Index: currentBlockIndex, + Index: intPtr(currentBlockIndex), }) currentBlockIndex++ inToolCall = true @@ -386,7 +386,7 @@ func handleAnthropicStream(c echo.Context, id string, input *schema.AnthropicReq tc := toolCalls[i] sendAnthropicSSE(c, schema.AnthropicStreamEvent{ Type: "content_block_start", - Index: currentBlockIndex, + Index: intPtr(currentBlockIndex), ContentBlock: &schema.AnthropicContentBlock{ Type: "tool_use", ID: fmt.Sprintf("toolu_%s_%d", id, i), @@ -395,7 +395,7 @@ func handleAnthropicStream(c echo.Context, id string, input *schema.AnthropicReq }) sendAnthropicSSE(c, schema.AnthropicStreamEvent{ Type: "content_block_delta", - Index: currentBlockIndex, + Index: intPtr(currentBlockIndex), Delta: &schema.AnthropicStreamDelta{ Type: "input_json_delta", PartialJSON: tc.Arguments, @@ -403,7 +403,7 @@ func handleAnthropicStream(c echo.Context, id string, input *schema.AnthropicReq }) sendAnthropicSSE(c, schema.AnthropicStreamEvent{ Type: "content_block_stop", - Index: currentBlockIndex, + Index: intPtr(currentBlockIndex), }) currentBlockIndex++ } @@ -416,7 +416,7 @@ func handleAnthropicStream(c echo.Context, id string, input *schema.AnthropicReq if !inToolCall { sendAnthropicSSE(c, schema.AnthropicStreamEvent{ Type: "content_block_delta", - Index: 0, + Index: intPtr(0), Delta: &schema.AnthropicStreamDelta{ Type: "text_delta", Text: token, @@ -516,7 +516,7 @@ func handleAnthropicStream(c echo.Context, id string, input *schema.AnthropicReq // Close the text content block sendAnthropicSSE(c, schema.AnthropicStreamEvent{ Type: "content_block_stop", - Index: currentBlockIndex, + Index: intPtr(currentBlockIndex), }) currentBlockIndex++ inToolCall = true @@ -528,7 +528,7 @@ func handleAnthropicStream(c echo.Context, id string, input *schema.AnthropicReq } sendAnthropicSSE(c, schema.AnthropicStreamEvent{ Type: "content_block_start", - Index: currentBlockIndex, + Index: intPtr(currentBlockIndex), ContentBlock: &schema.AnthropicContentBlock{ Type: "tool_use", ID: toolCallID, @@ -537,7 +537,7 @@ func handleAnthropicStream(c echo.Context, id string, input *schema.AnthropicReq }) sendAnthropicSSE(c, schema.AnthropicStreamEvent{ Type: "content_block_delta", - Index: currentBlockIndex, + Index: intPtr(currentBlockIndex), Delta: &schema.AnthropicStreamDelta{ Type: "input_json_delta", PartialJSON: fc.Arguments, @@ -545,7 +545,7 @@ func handleAnthropicStream(c echo.Context, id string, input *schema.AnthropicReq }) sendAnthropicSSE(c, schema.AnthropicStreamEvent{ Type: "content_block_stop", - Index: currentBlockIndex, + Index: intPtr(currentBlockIndex), }) currentBlockIndex++ toolCallsEmitted++ @@ -557,7 +557,7 @@ func handleAnthropicStream(c echo.Context, id string, input *schema.AnthropicReq if !inToolCall { sendAnthropicSSE(c, schema.AnthropicStreamEvent{ Type: "content_block_stop", - Index: 0, + Index: intPtr(0), }) } @@ -598,6 +598,8 @@ func convertFuncsToOpenAITools(funcs functions.Functions) []functions.Tool { return tools } +func intPtr(i int) *int { return &i } + func sendAnthropicSSE(c echo.Context, event schema.AnthropicStreamEvent) { data, err := json.Marshal(event) if err != nil { diff --git a/core/schema/anthropic.go b/core/schema/anthropic.go index 2b2319188c42..182dbfc3625b 100644 --- a/core/schema/anthropic.go +++ b/core/schema/anthropic.go @@ -78,7 +78,7 @@ type AnthropicMessage struct { // AnthropicContentBlock represents a content block in an Anthropic message type AnthropicContentBlock struct { Type string `json:"type"` - Text string `json:"text,omitempty"` + Text string `json:"text"` Source *AnthropicImageSource `json:"source,omitempty"` ID string `json:"id,omitempty"` Name string `json:"name,omitempty"` @@ -116,7 +116,7 @@ type AnthropicUsage struct { // AnthropicStreamEvent represents a streaming event from the Anthropic API type AnthropicStreamEvent struct { Type string `json:"type"` - Index int `json:"index,omitempty"` + Index *int `json:"index,omitempty"` ContentBlock *AnthropicContentBlock `json:"content_block,omitempty"` Delta *AnthropicStreamDelta `json:"delta,omitempty"` Message *AnthropicStreamMessage `json:"message,omitempty"`