Skip to content

OpenAI message adapters missing implementation for AIMessage.tool_calls #414

@rhlarora84

Description

@rhlarora84

Checked other resources

  • This is a bug, not a usage question.
  • I added a clear and descriptive title that summarizes this issue.
  • I used the GitHub search to find a similar question and didn't find it.
  • I am sure that this is a bug in LangChain Community rather than my code.
  • The bug is not resolved by updating to the latest stable version of LangChain Community.
  • I read what a minimal reproducible example is (https://stackoverflow.com/help/minimal-reproducible-example).
  • I posted a self-contained, minimal, reproducible example. A maintainer can copy it and run it AS IS.

Example Code

See below

Error Message and Stack Trace (if applicable)

No response

Description

The following code snippet from langchain_community.adapters.openai does not handle the attribute AIMessageChunk.tool_calls || AIMessageChunk.tool_call_chunk and solely relies on AIMessage.additional_kwargs(). additional_kwargs no longer contains the tool calls with Langchain 1.0 upgrade.

def _convert_message_chunk(chunk: BaseMessageChunk, i: int) -> dict:
    _dict: Dict[str, Any] = {}
    if isinstance(chunk, AIMessageChunk):
        if i == 0:
            # Only shows up in the first chunk
            _dict["role"] = "assistant"
        if "function_call" in chunk.additional_kwargs:
            _dict["function_call"] = chunk.additional_kwargs["function_call"]
            # If the first chunk is a function call, the content is not empty string,
            # not missing, but None.
            if i == 0:
                _dict["content"] = None
        if "tool_calls" in chunk.additional_kwargs:
            _dict["tool_calls"] = chunk.additional_kwargs["tool_calls"]
            # If the first chunk is tool calls, the content is not empty string,
            # not missing, but None.
            if i == 0:
                _dict["content"] = None
        else:
            _dict["content"] = chunk.content
    else:
        raise ValueError(f"Got unexpected streaming chunk type: {type(chunk)}")
    # This only happens at the end of streams, and OpenAI returns as empty dict
    if _dict == {"content": ""}:
        _dict = {}
    return _dict

Here is the response from Langchain 1.0

Image

Same problem with convert_message_to_dict in the same file

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions