Skip to content

Commit 2849cfe

Browse files
committed
Consolidate bedrock chunk any behind documented ParsedChunk alias
1 parent 2ca5dcf commit 2849cfe

1 file changed

Lines changed: 14 additions & 9 deletions

File tree

packages/server-utils/src/integrations/tracing-channel/aws-sdk/services/bedrock-runtime.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,14 @@ interface TokenUsage {
3131
interface ConverseStreamOutput {
3232
messageStop?: { stopReason?: string };
3333
metadata?: { usage?: TokenUsage };
34-
[key: string]: any;
34+
[key: string]: unknown;
3535
}
3636

37+
// Streamed `InvokeModel` chunks and response bodies are model-family-specific JSON (titan, nova,
38+
// claude, llama, cohere, mistral); the record helpers probe the shapes defensively, so `any` instead
39+
// of one structural type per family.
40+
type ParsedChunk = any;
41+
3742
export class BedrockRuntimeServiceExtension implements ServiceExtension {
3843
public requestPreSpanHook(request: NormalizedRequest): RequestMetadata {
3944
switch (request.commandName) {
@@ -389,7 +394,7 @@ function setUsage(span: Span, usage: TokenUsage | undefined): void {
389394
}
390395
}
391396

392-
function parseChunk(bytes?: Uint8Array): any {
397+
function parseChunk(bytes?: Uint8Array): ParsedChunk {
393398
if (!bytes || !(bytes instanceof Uint8Array)) {
394399
return null;
395400
}
@@ -402,7 +407,7 @@ function parseChunk(bytes?: Uint8Array): any {
402407
}
403408
}
404409

405-
function recordNovaAttributes(parsedChunk: any, span: Span): void {
410+
function recordNovaAttributes(parsedChunk: ParsedChunk, span: Span): void {
406411
if (parsedChunk.metadata?.usage !== undefined) {
407412
if (parsedChunk.metadata?.usage.inputTokens !== undefined) {
408413
span.setAttribute(GEN_AI_USAGE_INPUT_TOKENS, parsedChunk.metadata.usage.inputTokens);
@@ -416,7 +421,7 @@ function recordNovaAttributes(parsedChunk: any, span: Span): void {
416421
}
417422
}
418423

419-
function recordClaudeAttributes(parsedChunk: any, span: Span): void {
424+
function recordClaudeAttributes(parsedChunk: ParsedChunk, span: Span): void {
420425
if (parsedChunk.message?.usage?.input_tokens !== undefined) {
421426
span.setAttribute(GEN_AI_USAGE_INPUT_TOKENS, parsedChunk.message.usage.input_tokens);
422427
}
@@ -428,7 +433,7 @@ function recordClaudeAttributes(parsedChunk: any, span: Span): void {
428433
}
429434
}
430435

431-
function recordTitanAttributes(parsedChunk: any, span: Span): void {
436+
function recordTitanAttributes(parsedChunk: ParsedChunk, span: Span): void {
432437
if (parsedChunk.inputTextTokenCount !== undefined) {
433438
span.setAttribute(GEN_AI_USAGE_INPUT_TOKENS, parsedChunk.inputTextTokenCount);
434439
}
@@ -440,7 +445,7 @@ function recordTitanAttributes(parsedChunk: any, span: Span): void {
440445
}
441446
}
442447

443-
function recordLlamaAttributes(parsedChunk: any, span: Span): void {
448+
function recordLlamaAttributes(parsedChunk: ParsedChunk, span: Span): void {
444449
if (parsedChunk.prompt_token_count !== undefined) {
445450
span.setAttribute(GEN_AI_USAGE_INPUT_TOKENS, parsedChunk.prompt_token_count);
446451
}
@@ -452,7 +457,7 @@ function recordLlamaAttributes(parsedChunk: any, span: Span): void {
452457
}
453458
}
454459

455-
function recordMistralAttributes(parsedChunk: any, span: Span): void {
460+
function recordMistralAttributes(parsedChunk: ParsedChunk, span: Span): void {
456461
if (parsedChunk.outputs?.[0]?.text !== undefined) {
457462
span.setAttribute(GEN_AI_USAGE_OUTPUT_TOKENS, Math.ceil(parsedChunk.outputs[0].text.length / 6));
458463
}
@@ -461,7 +466,7 @@ function recordMistralAttributes(parsedChunk: any, span: Span): void {
461466
}
462467
}
463468

464-
function recordCohereAttributes(parsedChunk: any, span: Span): void {
469+
function recordCohereAttributes(parsedChunk: ParsedChunk, span: Span): void {
465470
if (parsedChunk.generations?.[0]?.text !== undefined) {
466471
span.setAttribute(GEN_AI_USAGE_OUTPUT_TOKENS, Math.ceil(parsedChunk.generations[0].text.length / 6));
467472
}
@@ -470,7 +475,7 @@ function recordCohereAttributes(parsedChunk: any, span: Span): void {
470475
}
471476
}
472477

473-
function recordCohereRAttributes(parsedChunk: any, span: Span): void {
478+
function recordCohereRAttributes(parsedChunk: ParsedChunk, span: Span): void {
474479
if (parsedChunk.text !== undefined) {
475480
span.setAttribute(GEN_AI_USAGE_OUTPUT_TOKENS, Math.ceil(parsedChunk.text.length / 6));
476481
}

0 commit comments

Comments
 (0)