Skip to content

Commit af89c01

Browse files
committed
fix accumalting .text
1 parent 8dfd6c4 commit af89c01

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

packages/core/src/utils/anthropic-ai/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,9 @@ function addResponseAttributes(span: Span, response: AnthropicAiResponse, record
114114
toolCalls.push(item);
115115
}
116116
}
117-
118-
span.setAttributes({ [GEN_AI_RESPONSE_TOOL_CALLS_ATTRIBUTE]: JSON.stringify(toolCalls) });
117+
if (toolCalls.length > 0) {
118+
span.setAttributes({ [GEN_AI_RESPONSE_TOOL_CALLS_ATTRIBUTE]: JSON.stringify(toolCalls) });
119+
}
119120
}
120121
}
121122
// Completions.create

packages/core/src/utils/anthropic-ai/streaming.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,16 +134,22 @@ function handleContentBlockDelta(
134134
state: StreamingState,
135135
recordOutputs: boolean,
136136
): void {
137-
if (event.type !== 'content_block_delta' || typeof event.index !== 'number' || !event.delta) return;
137+
if (event.type !== 'content_block_delta' || !event.delta) return;
138138

139-
if ('partial_json' in event.delta && typeof event.delta.partial_json === 'string') {
139+
// Accumulate tool_use input JSON deltas only when we have an index and an active tool block
140+
if (
141+
typeof event.index === 'number' &&
142+
'partial_json' in event.delta &&
143+
typeof event.delta.partial_json === 'string'
144+
) {
140145
const active = state.activeToolBlocks[event.index];
141146
if (active) {
142147
active.inputJsonParts.push(event.delta.partial_json);
143148
}
144149
}
145150

146-
if (recordOutputs && event.delta.text) {
151+
// Accumulate streamed response text regardless of index
152+
if (recordOutputs && typeof event.delta.text === 'string') {
147153
state.responseTexts.push(event.delta.text);
148154
}
149155
}

0 commit comments

Comments
 (0)