Skip to content

Commit

Permalink
fix: OpenAI Chat Generator - do not create TextContent if content
Browse files Browse the repository at this point in the history
… is `None`; make Anthropic Chat Generator more robust (#129)

* openai: do not create empty text content if content is None

* test

* make anthropic more robust

* adjust existing test
  • Loading branch information
anakin87 authored Nov 7, 2024
1 parent 0af47fc commit b5be4b6
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def _convert_messages_to_anthropic_format(

anthropic_msg: Dict[str, Any] = {"role": message._role.value, "content": []}

if message.texts:
if message.texts and message.texts[0]:
anthropic_msg["content"].append({"type": "text", "text": message.texts[0]})
if message.tool_calls:
anthropic_msg["content"] += _convert_tool_calls_to_anthropic_format(message.tool_calls)
Expand Down
2 changes: 1 addition & 1 deletion haystack_experimental/components/generators/chat/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ def _convert_chat_completion_to_chat_message(self, completion: ChatCompletion, c
:return: The ChatMessage.
"""
message: ChatCompletionMessage = choice.message
text = message.content or ""
text = message.content
tool_calls = []
if openai_tool_calls := message.tool_calls:
for openai_tc in openai_tool_calls:
Expand Down
19 changes: 18 additions & 1 deletion test/components/generators/anthropic/test_anthropic.py
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,24 @@ def test_convert_message_to_anthropic_format(self):
],
)

messages = [
ChatMessage.from_assistant(
text="", # this should not happen, but we should handle it without errors
tool_calls=[ToolCall(id="123", tool_name="weather", arguments={"city": "Paris"})]
)
]
result = _convert_messages_to_anthropic_format(messages)
assert result == (
[],
[
{
"role": "assistant",
"content": [{"type": "tool_use", "id": "123", "name": "weather", "input": {"city": "Paris"}}],
}
],
)


tool_result = json.dumps({"weather": "sunny", "temperature": "25"})
messages = [
ChatMessage.from_tool(
Expand Down Expand Up @@ -721,7 +739,6 @@ def test_convert_message_to_anthropic_format_complex(self):
{
"role": "assistant",
"content": [
{"type": "text", "text": ""},
{"type": "tool_use", "id": "123", "name": "weather", "input": {"city": "Paris"}},
{"type": "tool_use", "id": "456", "name": "math", "input": {"expression": "2+2"}},
],
Expand Down
5 changes: 5 additions & 0 deletions test/components/generators/chat/test_openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,9 @@ def test_run_with_tools(self, tools):
assert len(response["replies"]) == 1
message = response["replies"][0]

assert not message.texts
assert not message.text

assert message.tool_calls
tool_call = message.tool_call
assert isinstance(tool_call, ToolCall)
Expand Down Expand Up @@ -641,6 +644,8 @@ def test_live_run_with_tools(self, tools):
assert len(results["replies"]) == 1
message = results["replies"][0]

assert not message.texts
assert not message.text
assert message.tool_calls
tool_call = message.tool_call
assert isinstance(tool_call, ToolCall)
Expand Down

0 comments on commit b5be4b6

Please sign in to comment.