Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 96913b8

Browse files
authored
Merge pull request openai#93 from mjunaidca/fix/chat-history-assistant-role
Fix: Add missing support for 'assistant' Role in Converter.items_to_messages used by Runner.run_sync
2 parents e86e5e2 + c09a225 commit 96913b8

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

src/agents/models/openai_chatcompletions.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -808,6 +808,13 @@ def ensure_assistant_message() -> ChatCompletionAssistantMessageParam:
808808
"content": cls.extract_text_content(content),
809809
}
810810
result.append(msg_developer)
811+
elif role == "assistant":
812+
flush_assistant_message()
813+
msg_assistant: ChatCompletionAssistantMessageParam = {
814+
"role": "assistant",
815+
"content": cls.extract_text_content(content),
816+
}
817+
result.append(msg_assistant)
811818
else:
812819
raise UserError(f"Unexpected role in easy_input_message: {role}")
813820

tests/test_openai_chatcompletions_converter.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,3 +393,38 @@ def test_unknown_object_errors():
393393
with pytest.raises(UserError, match="Unhandled item type or structure"):
394394
# Purposely ignore the type error
395395
_Converter.items_to_messages([TestObject()]) # type: ignore
396+
397+
398+
def test_assistant_messages_in_history():
399+
"""
400+
Test that assistant messages are added to the history.
401+
"""
402+
messages = _Converter.items_to_messages(
403+
[
404+
{
405+
"role": "user",
406+
"content": "Hello",
407+
},
408+
{
409+
"role": "assistant",
410+
"content": "Hello?",
411+
},
412+
{
413+
"role": "user",
414+
"content": "What was my Name?",
415+
},
416+
]
417+
)
418+
419+
assert messages == [
420+
{"role": "user", "content": "Hello"},
421+
{"role": "assistant", "content": "Hello?"},
422+
{"role": "user", "content": "What was my Name?"},
423+
]
424+
assert len(messages) == 3
425+
assert messages[0]["role"] == "user"
426+
assert messages[0]["content"] == "Hello"
427+
assert messages[1]["role"] == "assistant"
428+
assert messages[1]["content"] == "Hello?"
429+
assert messages[2]["role"] == "user"
430+
assert messages[2]["content"] == "What was my Name?"

0 commit comments

Comments
 (0)