Skip to content

Commit b7e3589

Browse files
authored
fix(anthropic): show null index when not present, default to 0 (#9225)
Signed-off-by: Ettore Di Giacinto <mudler@localai.io>
1 parent 716ddd6 commit b7e3589

2 files changed

Lines changed: 15 additions & 13 deletions

File tree

core/http/endpoints/anthropic/messages.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ func handleAnthropicStream(c echo.Context, id string, input *schema.AnthropicReq
357357
// Send initial content_block_start event
358358
contentBlockStart := schema.AnthropicStreamEvent{
359359
Type: "content_block_start",
360-
Index: currentBlockIndex,
360+
Index: intPtr(currentBlockIndex),
361361
ContentBlock: &schema.AnthropicContentBlock{Type: "text", Text: ""},
362362
}
363363
sendAnthropicSSE(c, contentBlockStart)
@@ -376,7 +376,7 @@ func handleAnthropicStream(c echo.Context, id string, input *schema.AnthropicReq
376376
if !inToolCall && currentBlockIndex == 0 {
377377
sendAnthropicSSE(c, schema.AnthropicStreamEvent{
378378
Type: "content_block_stop",
379-
Index: currentBlockIndex,
379+
Index: intPtr(currentBlockIndex),
380380
})
381381
currentBlockIndex++
382382
inToolCall = true
@@ -386,7 +386,7 @@ func handleAnthropicStream(c echo.Context, id string, input *schema.AnthropicReq
386386
tc := toolCalls[i]
387387
sendAnthropicSSE(c, schema.AnthropicStreamEvent{
388388
Type: "content_block_start",
389-
Index: currentBlockIndex,
389+
Index: intPtr(currentBlockIndex),
390390
ContentBlock: &schema.AnthropicContentBlock{
391391
Type: "tool_use",
392392
ID: fmt.Sprintf("toolu_%s_%d", id, i),
@@ -395,15 +395,15 @@ func handleAnthropicStream(c echo.Context, id string, input *schema.AnthropicReq
395395
})
396396
sendAnthropicSSE(c, schema.AnthropicStreamEvent{
397397
Type: "content_block_delta",
398-
Index: currentBlockIndex,
398+
Index: intPtr(currentBlockIndex),
399399
Delta: &schema.AnthropicStreamDelta{
400400
Type: "input_json_delta",
401401
PartialJSON: tc.Arguments,
402402
},
403403
})
404404
sendAnthropicSSE(c, schema.AnthropicStreamEvent{
405405
Type: "content_block_stop",
406-
Index: currentBlockIndex,
406+
Index: intPtr(currentBlockIndex),
407407
})
408408
currentBlockIndex++
409409
}
@@ -416,7 +416,7 @@ func handleAnthropicStream(c echo.Context, id string, input *schema.AnthropicReq
416416
if !inToolCall {
417417
sendAnthropicSSE(c, schema.AnthropicStreamEvent{
418418
Type: "content_block_delta",
419-
Index: 0,
419+
Index: intPtr(0),
420420
Delta: &schema.AnthropicStreamDelta{
421421
Type: "text_delta",
422422
Text: token,
@@ -516,7 +516,7 @@ func handleAnthropicStream(c echo.Context, id string, input *schema.AnthropicReq
516516
// Close the text content block
517517
sendAnthropicSSE(c, schema.AnthropicStreamEvent{
518518
Type: "content_block_stop",
519-
Index: currentBlockIndex,
519+
Index: intPtr(currentBlockIndex),
520520
})
521521
currentBlockIndex++
522522
inToolCall = true
@@ -528,7 +528,7 @@ func handleAnthropicStream(c echo.Context, id string, input *schema.AnthropicReq
528528
}
529529
sendAnthropicSSE(c, schema.AnthropicStreamEvent{
530530
Type: "content_block_start",
531-
Index: currentBlockIndex,
531+
Index: intPtr(currentBlockIndex),
532532
ContentBlock: &schema.AnthropicContentBlock{
533533
Type: "tool_use",
534534
ID: toolCallID,
@@ -537,15 +537,15 @@ func handleAnthropicStream(c echo.Context, id string, input *schema.AnthropicReq
537537
})
538538
sendAnthropicSSE(c, schema.AnthropicStreamEvent{
539539
Type: "content_block_delta",
540-
Index: currentBlockIndex,
540+
Index: intPtr(currentBlockIndex),
541541
Delta: &schema.AnthropicStreamDelta{
542542
Type: "input_json_delta",
543543
PartialJSON: fc.Arguments,
544544
},
545545
})
546546
sendAnthropicSSE(c, schema.AnthropicStreamEvent{
547547
Type: "content_block_stop",
548-
Index: currentBlockIndex,
548+
Index: intPtr(currentBlockIndex),
549549
})
550550
currentBlockIndex++
551551
toolCallsEmitted++
@@ -557,7 +557,7 @@ func handleAnthropicStream(c echo.Context, id string, input *schema.AnthropicReq
557557
if !inToolCall {
558558
sendAnthropicSSE(c, schema.AnthropicStreamEvent{
559559
Type: "content_block_stop",
560-
Index: 0,
560+
Index: intPtr(0),
561561
})
562562
}
563563

@@ -598,6 +598,8 @@ func convertFuncsToOpenAITools(funcs functions.Functions) []functions.Tool {
598598
return tools
599599
}
600600

601+
func intPtr(i int) *int { return &i }
602+
601603
func sendAnthropicSSE(c echo.Context, event schema.AnthropicStreamEvent) {
602604
data, err := json.Marshal(event)
603605
if err != nil {

core/schema/anthropic.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ type AnthropicMessage struct {
7878
// AnthropicContentBlock represents a content block in an Anthropic message
7979
type AnthropicContentBlock struct {
8080
Type string `json:"type"`
81-
Text string `json:"text,omitempty"`
81+
Text string `json:"text"`
8282
Source *AnthropicImageSource `json:"source,omitempty"`
8383
ID string `json:"id,omitempty"`
8484
Name string `json:"name,omitempty"`
@@ -116,7 +116,7 @@ type AnthropicUsage struct {
116116
// AnthropicStreamEvent represents a streaming event from the Anthropic API
117117
type AnthropicStreamEvent struct {
118118
Type string `json:"type"`
119-
Index int `json:"index,omitempty"`
119+
Index *int `json:"index,omitempty"`
120120
ContentBlock *AnthropicContentBlock `json:"content_block,omitempty"`
121121
Delta *AnthropicStreamDelta `json:"delta,omitempty"`
122122
Message *AnthropicStreamMessage `json:"message,omitempty"`

0 commit comments

Comments
 (0)