mirror of
https://github.com/BerriAI/litellm.git
synced 2026-09-10 22:41:41 +00:00
fix(cohere): address review suggestions
- Guard data["messages"] write with 'if messages in data' to avoid injecting an empty list when parent transform omits the key - Add hasattr(tc, "model_dump") guard before calling model_dump on non-dict tool calls to avoid AttributeError on unknown types - Assert type and function fields are preserved after Pydantic model_dump
This commit is contained in:
parent
85ee75c576
commit
fc68752d80
2 changed files with 7 additions and 1 deletions
|
|
@ -179,8 +179,10 @@ class CohereV2ChatConfig(OpenAIGPTConfig):
|
|||
# Cohere v2 rejects fields that are valid in OpenAI but not in Cohere:
|
||||
# 1. 'index' in assistant tool_calls
|
||||
# 2. 'name' in tool result messages
|
||||
if "messages" not in data:
|
||||
return data
|
||||
sanitized: List[AllMessageValues] = []
|
||||
for message in data.get("messages", []):
|
||||
for message in data["messages"]:
|
||||
if hasattr(message, "model_dump"):
|
||||
message = message.model_dump(exclude_unset=True)
|
||||
if isinstance(message, dict):
|
||||
|
|
@ -190,6 +192,8 @@ class CohereV2ChatConfig(OpenAIGPTConfig):
|
|||
{k: v for k, v in tc.items() if k != "index"}
|
||||
if isinstance(tc, dict)
|
||||
else {k: v for k, v in tc.model_dump(exclude_unset=True).items() if k != "index"}
|
||||
if hasattr(tc, "model_dump")
|
||||
else tc
|
||||
for tc in message["tool_calls"]
|
||||
]
|
||||
message = {**message, "tool_calls": cleaned_tool_calls}
|
||||
|
|
|
|||
|
|
@ -133,6 +133,8 @@ class TestCohereV2Transform:
|
|||
assistant_msg = result["messages"][1]
|
||||
assert "index" not in assistant_msg["tool_calls"][0]
|
||||
assert assistant_msg["tool_calls"][0]["id"] == "call_abc"
|
||||
assert assistant_msg["tool_calls"][0]["type"] == "function"
|
||||
assert assistant_msg["tool_calls"][0]["function"]["name"] == "get_time"
|
||||
|
||||
def test_preserves_messages_without_offending_fields(self):
|
||||
"""Messages that don't have index or name are passed through unchanged."""
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue