Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,16 @@ def _is_huggingface_hub(llm: Any) -> bool:
def _convert_chunk_to_message_chunk(
chunk: Mapping[str, Any], default_class: type[BaseMessageChunk]
) -> BaseMessageChunk:
if hasattr(chunk, "choices"):
choice_obj = chunk.choices[0]
delta_obj = choice_obj.delta
reasoning_content_value = getattr(delta_obj, "reasoning_content", None)
else:
reasoning_content_value = None

choice = chunk["choices"][0]
_dict = choice["delta"]

role = cast(str, _dict.get("role"))
content = cast(str, _dict.get("content") or "")
additional_kwargs: dict = {}
Expand All @@ -258,6 +266,9 @@ def _convert_chunk_to_message_chunk(
if role == "user" or default_class == HumanMessageChunk:
return HumanMessageChunk(content=content)
if role == "assistant" or default_class == AIMessageChunk:
reasoning = _dict.get("reasoning") or reasoning_content_value
additional_kwargs["reasoning_content"] = reasoning

if usage := chunk.get("usage"):
input_tokens = usage.get("prompt_tokens", 0)
output_tokens = usage.get("completion_tokens", 0)
Expand Down