fix(anthropic): surface in-band stream error + Builder accumulation (R7c)
This commit is contained in:
1 parent
d0cd0989a9
commit
373e44b9c7
2 files changed
+26
-6
No files matched your search
@@ -4,6 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@@ -164,8 +165,8 @@ func (p *AnthropicProvider) ChatStream(ctx context.Context, req llm.ChatRequest)
|
|||||||
type pendingTC struct {
|
type pendingTC struct {
|
||||||
id string
|
id string
|
||||||
name string
|
name string
|
||||||
inputJSON string // built from input_json_delta events
|
inputJSON strings.Builder // built from input_json_delta events
|
||||||
startInput string // captured from content_block_start (proxy fallback)
|
startInput string // captured from content_block_start (proxy fallback)
|
||||||
}
|
}
|
||||||
var pendingTools []pendingTC
|
var pendingTools []pendingTC
|
||||||
var currentBlockType string
|
var currentBlockType string
|
||||||
@@ -220,18 +221,21 @@ func (p *AnthropicProvider) ChatStream(ctx context.Context, req llm.ChatRequest)
|
|||||||
}
|
}
|
||||||
case "input_json_delta":
|
case "input_json_delta":
|
||||||
if len(pendingTools) > 0 {
|
if len(pendingTools) > 0 {
|
||||||
pendingTools[len(pendingTools)-1].inputJSON += delta.PartialJSON
|
pendingTools[len(pendingTools)-1].inputJSON.WriteString(delta.PartialJSON)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
case "content_block_stop":
|
case "content_block_stop":
|
||||||
if currentBlockType == "tool_use" && len(pendingTools) > 0 {
|
if currentBlockType == "tool_use" && len(pendingTools) > 0 {
|
||||||
tc := pendingTools[len(pendingTools)-1]
|
// Index in place rather than copying the element: the
|
||||||
|
// pendingTC holds a strings.Builder, which must not be
|
||||||
|
// copied after first write (vet copylock).
|
||||||
|
tc := &pendingTools[len(pendingTools)-1]
|
||||||
// Prefer streamed deltas (canonical Anthropic). Fall
|
// Prefer streamed deltas (canonical Anthropic). Fall
|
||||||
// back to start-time input (proxy emitting full input
|
// back to start-time input (proxy emitting full input
|
||||||
// upfront). Default to "{}" so MCP servers receive a
|
// upfront). Default to "{}" so MCP servers receive a
|
||||||
// valid empty-args envelope rather than null/empty.
|
// valid empty-args envelope rather than null/empty.
|
||||||
inp := tc.inputJSON
|
inp := tc.inputJSON.String()
|
||||||
if inp == "" {
|
if inp == "" {
|
||||||
inp = tc.startInput
|
inp = tc.startInput
|
||||||
}
|
}
|
||||||
@@ -281,7 +285,14 @@ func (p *AnthropicProvider) ChatStream(ctx context.Context, req llm.ChatRequest)
|
|||||||
// Final event — nothing extra to emit
|
// Final event — nothing extra to emit
|
||||||
|
|
||||||
case "error":
|
case "error":
|
||||||
slog.Error("anthropic stream error", "event", event.Type)
|
// The SDK surfaces error detail via stream.Err() (handled after
|
||||||
|
// the loop); this in-band branch must not be a silent no-op —
|
||||||
|
// emit an EventError so the runtime treats it as a failure.
|
||||||
|
slog.Error("anthropic stream error event", "event", event.Type)
|
||||||
|
events <- llm.ChatEvent{
|
||||||
|
Type: llm.EventError,
|
||||||
|
Error: fmt.Errorf("anthropic stream error event"),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
anthropic "github.com/anthropics/anthropic-sdk-go"
|
anthropic "github.com/anthropics/anthropic-sdk-go"
|
||||||
@@ -708,3 +709,11 @@ data: {"type":"message_stop"}
|
|||||||
assert.Equal(t, "end_turn", done.StopReason)
|
assert.Equal(t, "end_turn", done.StopReason)
|
||||||
assert.Empty(t, done.StopCategory)
|
assert.Empty(t, done.StopCategory)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestAnthropicInputAccumulation(t *testing.T) {
|
||||||
|
var b strings.Builder
|
||||||
|
for _, part := range []string{`{"a"`, `:1`, `}`} {
|
||||||
|
b.WriteString(part)
|
||||||
|
}
|
||||||
|
require.Equal(t, `{"a":1}`, b.String())
|
||||||
|
}
|
||||||
Reference in new issue
Block a user