Skip to content

Commit ebc766e

Browse files
authored
fix: update Cohere to client 5.16.0 (#2086)
1 parent cd849c9 commit ebc766e

2 files changed

Lines changed: 17 additions & 13 deletions

File tree

integrations/cohere/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ classifiers = [
2323
"Programming Language :: Python :: Implementation :: CPython",
2424
"Programming Language :: Python :: Implementation :: PyPy",
2525
]
26-
dependencies = ["haystack-ai>=2.15.1", "cohere>=5.12.0,<5.16.0"]
26+
dependencies = ["haystack-ai>=2.15.1", "cohere>=5.16.0"]
2727

2828
[project.urls]
2929
Documentation = "https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/cohere#readme"

integrations/cohere/src/haystack_integrations/components/generators/cohere/chat/chat_generator.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@
4242
ClientV2,
4343
StreamedChatResponseV2,
4444
SystemChatMessageV2,
45-
TextAssistantMessageContentItem,
45+
TextAssistantMessageV2ContentItem,
4646
TextContent,
47-
TextSystemMessageContentItem,
47+
TextSystemMessageV2ContentItem,
4848
ToolCallV2,
4949
ToolCallV2Function,
5050
ToolChatMessageV2,
@@ -127,9 +127,9 @@ def _format_message(
127127
if message.role.value == "user":
128128
return UserChatMessageV2(content=[TextContent(text=message.texts[0])])
129129
elif message.role.value == "assistant":
130-
return AssistantChatMessageV2(content=[TextAssistantMessageContentItem(text=message.texts[0])])
130+
return AssistantChatMessageV2(content=[TextAssistantMessageV2ContentItem(text=message.texts[0])])
131131
elif message.role.value == "system":
132-
return SystemChatMessageV2(content=[TextSystemMessageContentItem(text=message.texts[0])])
132+
return SystemChatMessageV2(content=[TextSystemMessageV2ContentItem(text=message.texts[0])])
133133
else:
134134
msg = f"Unsupported message role: {message.role.value}"
135135
raise ValueError(msg)
@@ -274,21 +274,25 @@ def _process_cohere_chunk(cohere_chunk: StreamedChatResponseV2, state: Dict[str,
274274
state["current_tool_call"] = None
275275
state["current_tool_arguments"] = ""
276276

277+
usage_data = getattr(cohere_chunk.delta, "usage", None)
278+
finish_reason = getattr(cohere_chunk.delta, "finish_reason", None)
279+
277280
if (
278-
cohere_chunk.delta.finish_reason is not None
279-
and cohere_chunk.delta.usage
280-
and cohere_chunk.delta.usage.billed_units
281-
and cohere_chunk.delta.usage.billed_units.input_tokens is not None
282-
and cohere_chunk.delta.usage.billed_units.output_tokens is not None
281+
finish_reason is not None
282+
and usage_data is not None
283+
and isinstance(usage_data, dict)
284+
and "billed_units" in usage_data
285+
and "input_tokens" in usage_data["billed_units"]
286+
and "output_tokens" in usage_data["billed_units"]
283287
):
284288
state["captured_meta"].update(
285289
{
286290
"model": model,
287291
"index": 0,
288-
"finish_reason": cohere_chunk.delta.finish_reason,
292+
"finish_reason": finish_reason,
289293
"usage": {
290-
"prompt_tokens": cohere_chunk.delta.usage.billed_units.input_tokens,
291-
"completion_tokens": cohere_chunk.delta.usage.billed_units.output_tokens,
294+
"prompt_tokens": usage_data["billed_units"]["input_tokens"],
295+
"completion_tokens": usage_data["billed_units"]["output_tokens"],
292296
},
293297
}
294298
)

0 commit comments

Comments
 (0)