Skip to content

Commit 6af4c33

Browse files
nirgaclaude
andcommitted
fix: correct Cohere response parsing for finish_reason
Fix Cohere response attribute extraction to access nested generations array for finish_reason and text fields. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent 656265d commit 6af4c33

File tree

2 files changed

+17
-14
lines changed

2 files changed

+17
-14
lines changed

packages/instrumentation-bedrock/src/instrumentation.ts

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -351,10 +351,11 @@ export class BedrockInstrumentation extends InstrumentationBase {
351351
if (requestBody["messages"]) {
352352
const promptAttributes: Record<string, any> = {};
353353
requestBody["messages"].forEach((message: any, index: number) => {
354-
promptAttributes[`${SpanAttributes.LLM_PROMPTS}.${index}.role`] = message.role;
355-
promptAttributes[`${SpanAttributes.LLM_PROMPTS}.${index}.content`] =
356-
typeof message.content === "string"
357-
? message.content
354+
promptAttributes[`${SpanAttributes.LLM_PROMPTS}.${index}.role`] =
355+
message.role;
356+
promptAttributes[`${SpanAttributes.LLM_PROMPTS}.${index}.content`] =
357+
typeof message.content === "string"
358+
? message.content
358359
: JSON.stringify(message.content);
359360
});
360361
return { ...baseAttributes, ...promptAttributes };
@@ -469,7 +470,7 @@ export class BedrockInstrumentation extends InstrumentationBase {
469470

470471
// Handle new messages API format response
471472
if (response["content"]) {
472-
const content = Array.isArray(response["content"])
473+
const content = Array.isArray(response["content"])
473474
? response["content"].map((c: any) => c.text || c).join("")
474475
: response["content"];
475476
return {
@@ -482,7 +483,8 @@ export class BedrockInstrumentation extends InstrumentationBase {
482483
if (response["completion"]) {
483484
return {
484485
...baseAttributes,
485-
[`${SpanAttributes.LLM_COMPLETIONS}.0.content`]: response["completion"],
486+
[`${SpanAttributes.LLM_COMPLETIONS}.0.content`]:
487+
response["completion"],
486488
};
487489
}
488490

@@ -491,12 +493,12 @@ export class BedrockInstrumentation extends InstrumentationBase {
491493
case "cohere": {
492494
const baseAttributes = {
493495
[`${SpanAttributes.LLM_COMPLETIONS}.0.finish_reason`]:
494-
response["finish_reason"],
496+
response["generations"]?.[0]?.["finish_reason"],
495497
[`${SpanAttributes.LLM_COMPLETIONS}.0.role`]: "assistant",
496498
...(this._shouldSendPrompts()
497499
? {
498500
[`${SpanAttributes.LLM_COMPLETIONS}.0.content`]:
499-
response["text"],
501+
response["generations"]?.[0]?.["text"],
500502
}
501503
: {}),
502504
};
@@ -506,10 +508,13 @@ export class BedrockInstrumentation extends InstrumentationBase {
506508
const billedUnits = response["meta"]["billed_units"];
507509
return {
508510
...baseAttributes,
509-
[SpanAttributes.LLM_USAGE_PROMPT_TOKENS]: billedUnits["input_tokens"],
510-
[SpanAttributes.LLM_USAGE_COMPLETION_TOKENS]: billedUnits["output_tokens"],
511-
[SpanAttributes.LLM_USAGE_TOTAL_TOKENS]:
512-
(billedUnits["input_tokens"] || 0) + (billedUnits["output_tokens"] || 0),
511+
[SpanAttributes.LLM_USAGE_PROMPT_TOKENS]:
512+
billedUnits["input_tokens"],
513+
[SpanAttributes.LLM_USAGE_COMPLETION_TOKENS]:
514+
billedUnits["output_tokens"],
515+
[SpanAttributes.LLM_USAGE_TOTAL_TOKENS]:
516+
(billedUnits["input_tokens"] || 0) +
517+
(billedUnits["output_tokens"] || 0),
513518
};
514519
}
515520

packages/instrumentation-langchain/src/instrumentation.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ export class LangChainInstrumentation extends InstrumentationBase {
2828
constructor(config: LangChainInstrumentationConfig = {}) {
2929
super("@traceloop/instrumentation-langchain", version, config);
3030

31-
3231
// Manually instrument CallbackManager immediately since module detection doesn't work
3332
this.instrumentCallbackManagerDirectly();
3433
}
@@ -83,7 +82,6 @@ export class LangChainInstrumentation extends InstrumentationBase {
8382
inheritableMetadata?: Record<string, unknown>,
8483
localMetadata?: Record<string, unknown>,
8584
) {
86-
8785
// Add our callback handler to inheritable handlers
8886
const callbackHandler = new TraceloopCallbackHandler(
8987
self.tracer,

0 commit comments

Comments
 (0)