Skip to content

fix: convert litellm response with reasoning content to openai message #1098

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/agents/extensions/models/litellm_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,13 +364,18 @@ def convert_message_to_openai(
provider_specific_fields.get("refusal", None) if provider_specific_fields else None
)

reasoning_content = ""
if hasattr(message, "reasoning_content") and message.reasoning_content:
reasoning_content = message.reasoning_content

return ChatCompletionMessage(
content=message.content,
refusal=refusal,
role="assistant",
annotations=cls.convert_annotations_to_openai(message),
audio=message.get("audio", None), # litellm deletes audio if not present
tool_calls=tool_calls,
reasoning_content=reasoning_content,
)

@classmethod
Expand Down
13 changes: 12 additions & 1 deletion src/agents/models/chatcmpl_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
ResponseOutputRefusal,
ResponseOutputText,
ResponseReasoningItem,
ResponseReasoningItemParam,
)
from openai.types.responses.response_input_param import FunctionCallOutput, ItemReference, Message
from openai.types.responses.response_reasoning_item import Summary
Expand Down Expand Up @@ -207,6 +208,12 @@ def maybe_response_output_message(cls, item: Any) -> ResponseOutputMessageParam
return cast(ResponseOutputMessageParam, item)
return None

@classmethod
def maybe_reasoning_message(cls, item: Any) -> ResponseReasoningItemParam | None:
if isinstance(item, dict) and item.get("type") == "reasoning":
return cast(ResponseReasoningItemParam, item)
return None

@classmethod
def extract_text_content(
cls, content: str | Iterable[ResponseInputContentParam]
Expand Down Expand Up @@ -456,7 +463,11 @@ def ensure_assistant_message() -> ChatCompletionAssistantMessageParam:
f"Encountered an item_reference, which is not supported: {item_ref}"
)

# 7) If we haven't recognized it => fail or ignore
# 7) reasoning message => not handled
elif cls.maybe_reasoning_message(item):
pass

# 8) If we haven't recognized it => fail or ignore
else:
raise UserError(f"Unhandled item type or structure: {item}")

Expand Down