Skip to content

Conversation

@dip9811111
Copy link

This PR enhances the HuggingFace integration by including reasoning_content extracted from the model response in AIMessageChunk.additional_kwargs.

Previously, AIMessageChunk did not expose the "reasoning" or "reasoning_content" field returned by HuggingFace models, making it inaccessible to downstream LangChain components.
This change ensures that any "reasoning" or "reasoning_content" value in the provider response is propagated and available to users.

The updated logic inside the HuggingFace formatter now includes:

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"]

    [...]

    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)
            usage_metadata = {
                "input_tokens": input_tokens,
                "output_tokens": output_tokens,
                "total_tokens": usage.get("total_tokens", input_tokens + output_tokens),
            }
        else:
            usage_metadata = None
        return AIMessageChunk(
            content=content,
            additional_kwargs=additional_kwargs,
            tool_call_chunks=tool_call_chunks,
            usage_metadata=usage_metadata,  # type: ignore[arg-type]
        )
    [...]

This makes reasoning traces accessible to agents, logging utilities, tracing tools, and any consumer of AIMessageChunk.

Issue

(#34011)

Dependencies

No new dependencies required.

@github-actions github-actions bot added integration Related to a provider partner package integration huggingface feature labels Nov 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature huggingface integration Related to a provider partner package integration

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants