openai-python icon indicating copy to clipboard operation
openai-python copied to clipboard

`ChatCompletionToolMessageParam` should have an optional `name` property

Open rgbkrk opened this issue 1 year ago • 2 comments

Confirm this is an issue with the Python library and not an underlying OpenAI API

  • [X] This is an issue with the Python library

Describe the bug

In the docs for parallel function calling (https://platform.openai.com/docs/guides/function-calling), the name is included along with the tool_call_id, content, and role. All the other messages have a name.

        ... # rest of the example from https://platform.openai.com/docs/guides/function-calling

        for tool_call in tool_calls:
            function_name = tool_call.function.name
            function_to_call = available_functions[function_name]
            function_args = json.loads(tool_call.function.arguments)
            function_response = function_to_call(
                location=function_args.get("location"),
                unit=function_args.get("unit"),
            )
            messages.append(
                {
                    "tool_call_id": tool_call.id,
                    "role": "tool",
                    "name": function_name,
                    "content": function_response,
                }
            )  # extend conversation with function response
        second_response = client.chat.completions.create(
            model="gpt-3.5-turbo-1106",
            messages=messages,
        )  # get a new response from the model where it can see the function response
        return second_response

Either the docs need an update or ChatCompletionToolMessageParam needs name (even if Optional).

To Reproduce

Run the following script through mypy

from openai.types.chat import ChatCompletionToolMessageParam

def tool_result(tool_call_id: str, name: str, content: str) -> ChatCompletionToolMessageParam:
    """Create a tool result message.

    Args:
        tool_call_id: The ID of the tool call.
        name: The name of the tool.
        content: The content of the message.

    Returns:
        A dictionary representing a tool result message.
    """
    return {
        "role": "tool",
        "content": content,
        "name": name,
        "tool_call_id": tool_call_id,
    }

tool_result("tool_pretend_id", name="myfunc", content="4")

Code snippets

from openai.types.chat import ChatCompletionToolMessageParam

def tool_result(tool_call_id: str, name: str, content: str) -> ChatCompletionToolMessageParam:
    """Create a tool result message.

    Args:
        tool_call_id: The ID of the tool call.
        name: The name of the tool.
        content: The content of the message.

    Returns:
        A dictionary representing a tool result message.
    """
    return {
        "role": "tool",
        "content": content,
        "name": name,
        "tool_call_id": tool_call_id,
    }

tool_result("tool_pretend_id", name="myfunc", content="4")

OS

macOS

Python version

Python 3.12.1

Library version

openai v1.4.0

rgbkrk avatar Jan 16 '24 19:01 rgbkrk

@logankilpatrick is this correct?

rattrayalex avatar Jan 24 '24 02:01 rattrayalex

Unless I'm mistaken, is name not going to be in the TypedDict? I don't see it in https://github.com/openai/openai-python/blob/main/src/openai/types/chat/chat_completion_tool_message_param.py

rgbkrk avatar Mar 04 '24 22:03 rgbkrk