# A correct message in response to a tool call cannot validate as ChatCompletionMessage

**URL:** <https://community.openai.com/t/a-correct-message-in-response-to-a-tool-call-cannot-validate-as-chatcompletionmessage/725036>\
**Category:** API\
**Created:** [April 22, 2024, 8:51pm UTC](https://community.openai.com/t/a-correct-message-in-response-to-a-tool-call-cannot-validate-as-chatcompletionmessage/725036 "2024-04-22T20:51:52Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![zzbbyy](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/zzbbyy/32/520007_2.png) [@zzbbyy](https://community.openai.com/u/zzbbyy)\
**Post date:** [April 22, 2024, 8:51pm UTC](https://community.openai.com/t/a-correct-message-in-response-to-a-tool-call-cannot-validate-as-chatcompletionmessage/725036/1 "2024-04-22T20:51:52Z")

</div>

In response to a tool call we need to create the following message:

```auto
{
      "tool_call_id": tool_call.id,
      "role": "tool",
      "name": function_name,
      "content": function_response,
}

```

This is copied from [https://platform.openai.com/docs/guides/function-calling](https://platform.openai.com/docs/guides/function-calling) under **Example invoking multiple function calls in one response** and this seems to work.  
But this structure does not validate as a ChatCompletionMessage:

```auto
from openai.types.chat.chat_completion import ChatCompletionMessage

message_dict = {
     "tool_call_id": 'tool_call.id',
    "role": "tool",
    "name": 'function_name',
    "content": 'function_response',
}

message = ChatCompletionMessage(**message_dict)

```

Output:

```auto
Traceback (most recent call last):
  File "/home/zby/gpt/answerbot/tmp/ChatCompletionMessage_example.py", line 10, in <module>
    message = ChatCompletionMessage(**message_dict)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/zby/gpt/answerbot/.venv/lib/python3.11/site-packages/pydantic/main.py", line 175, in __init__
    self. __pydantic_validator__.validate_python(data, self_instance=self)
pydantic_core._pydantic_core.ValidationError: 1 validation error for ChatCompletionMessage
role
  Input should be 'assistant' [type=literal_error, input_value='tool', input_type=str]
    For further information visit https://errors.pydantic.dev/2.7/v/literal_error

```

If I change the role to ‘assistant’ - then it seems to work.

---

<div class="post-metadata">

**Author:** ![anon22939549](https://avatars.discourse-cdn.com/v4/letter/a/f14d63/32.png) [@anon22939549](https://community.openai.com/u/anon22939549)\
**Post date:** [April 23, 2024, 5:18am UTC](https://community.openai.com/t/a-correct-message-in-response-to-a-tool-call-cannot-validate-as-chatcompletionmessage/725036/2 "2024-04-23T05:18:25Z")

</div>

That is because you aren’t using it in the same manner as is presented in the example.

This is invalid,

```python
message = ChatCompletionMessage(**message_dict)

```

This is what the documentation does,

```python
tool_calls = response_message.tool_calls
    # Step 2: check if the model wanted to call a function
    if tool_calls:
        # Step 3: call the function
        # Note: the JSON response may not always be valid; be sure to handle errors
        available_functions = {
            "get_current_weather": get_current_weather,
        } # only one function in this example, but you can have multiple
        messages.append(response_message) # extend conversation with assistant's reply
        # Step 4: send the info for each function call and function response to the model
        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-0125",
            messages=messages,
        ) # get a new response from the model where it can see the function response
        return second_response

```

You need to use the `.append` method to add the function response message to the existing messages.

---

<div class="post-metadata">

**Author:** ![\_j](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/_j/32/766292_2.png) [@\_j](https://community.openai.com/u/_j)\
**Post date:** [April 23, 2024, 5:40am UTC](https://community.openai.com/t/a-correct-message-in-response-to-a-tool-call-cannot-validate-as-chatcompletionmessage/725036/3 "2024-04-23T05:40:55Z")

</div>

> [@anon22939549](#):
>
> This is invalid,
> 
> `message = ChatCompletionMessage(**message_dict)`

That uses dictionary to parameter unpacking, a perfectly valid method of passing a JSON-like Python object into a function as parameters.

It uses one of “types” from the Python library, something I haven’t messed with, because a python list is just fine.

However, the input to the API chat.competions function is ALL parameters of the API call, not just the messages. Things like the model name, temperature, the tool specification, etc. Of which the messages (a list) is just one more thing that must be present.

I use that convention in my big demo of returning the assistant message with a tool call and the paired tool call return with matching IDs.

> [@Function Calling Help - Model Doesn't Seem To Accept Function Prompt?](https://community.openai.com/t/function-calling-help-model-doesnt-seem-to-accept-function-prompt/558935/11):
>
> I decided to make the mega-demo. Going beyond this I might as well just write a chatbot with classes for handling simulated functions and showing rewriting them to a real API… # imports and set up the OpenAI client object with a shorter timeout from openai import OpenAI import json client = OpenAI(timeout=30) # Here we'll make a tool specification, more flexible by adding one at a time toolspec=[] toolspec.extend([{ "type": "function", "function": { "name": "get\_we…

What I wrote is a linear example with fixed strings to be as demonstrative as possible. You must have the parsing that looks for multiple parallel tools, and adaptively provide a list of all of them in the return. That’s the purpose of the “for tool\_call” in the code sample above.

Also, the endpoint will give you an error if you don’t have both “assistant” and “tool”, with matching IDs and length.

---

<div class="post-metadata">

**Author:** ![anon22939549](https://avatars.discourse-cdn.com/v4/letter/a/f14d63/32.png) [@anon22939549](https://community.openai.com/u/anon22939549)\
**Post date:** [April 23, 2024, 5:50am UTC](https://community.openai.com/t/a-correct-message-in-response-to-a-tool-call-cannot-validate-as-chatcompletionmessage/725036/4 "2024-04-23T05:50:34Z")

</div>

> [@\_j](#):
>
> > [@anon22939549](#):
> >
> > This is invalid,
> > 
> > `message = ChatCompletionMessage(**message_dict)`
> 
> That uses dictionary to parameter unpacking, a perfectly valid method of passing a JSON-like Python object into a function as parameters.

Sorry @_j, I’ll be more clear.

This is invalid,

```auto
message = ChatCompletionMessage(**message_dict)

```

With respect to the OpenAI Python module given the content which the OP has demonstrated is contained within the `message_dict`.

Specifically, using a `role` of `tool`.

Is there anything else you want to be needlessly pedantic about right now?

---

<div class="post-metadata">

**Author:** ![\_j](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/_j/32/766292_2.png) [@\_j](https://community.openai.com/u/_j)\
**Post date:** [April 23, 2024, 6:19am UTC](https://community.openai.com/t/a-correct-message-in-response-to-a-tool-call-cannot-validate-as-chatcompletionmessage/725036/5 "2024-04-23T06:19:54Z")

</div>

I can be Pydantic.

```python
print(json.dumps(typ.chat.chat_completion.ChatCompletionMessage.model_json_schema(), indent=1))`

{
 "$defs": {
  "ChatCompletionMessageToolCall": {
   "additionalProperties": true,
   "properties": {
    "id": {
     "title": "Id",
     "type": "string"
    },
    "function": {
     "$ref": "#/$defs/Function"
    },
    "type": {
     "const": "function",
     "title": "Type"
    }
   },
   "required": [
    "id",
    "function",
    "type"
   ],
   "title": "ChatCompletionMessageToolCall",
   "type": "object"
  },
  "Function": {
   "additionalProperties": true,
   "properties": {
    "arguments": {
     "title": "Arguments",
     "type": "string"
    },
    "name": {
     "title": "Name",
     "type": "string"
    }
   },
   "required": [
    "arguments",
    "name"
   ],
   "title": "Function",
   "type": "object"
  },
  "FunctionCall": {
   "additionalProperties": true,
   "properties": {
    "arguments": {
     "title": "Arguments",
     "type": "string"
    },
    "name": {
     "title": "Name",
     "type": "string"
    }
   },
   "required": [
    "arguments",
    "name"
   ],
   "title": "FunctionCall",
   "type": "object"
  }
 },
 "additionalProperties": true,
 "properties": {
  "content": {
   "anyOf": [
    {
     "type": "string"
    },
    {
     "type": "null"
    }
   ],
   "default": null,
   "title": "Content"
  },
  "role": {
   "const": "assistant",
   "title": "Role"
  },
  "function_call": {
   "anyOf": [
    {
     "$ref": "#/$defs/FunctionCall"
    },
    {
     "type": "null"
    }
   ],
   "default": null
  },
  "tool_calls": {
   "anyOf": [
    {
     "items": {
      "$ref": "#/$defs/ChatCompletionMessageToolCall"
     },
     "type": "array"
    },
    {
     "type": "null"
    }
   ],
   "default": null,
   "title": "Tool Calls"
  }
 },
 "required": [
  "role"
 ],
 "title": "ChatCompletionMessage",
 "type": "object"
}

```

* * *

From types, this object class appears to be for capturing the **response** from the API. That’s the reason anything but “assistant” doesn’t validate. Accompanying this level in the typing hierarchy are schemas such as “ChatCompletionTokenLogprob”, which is certainly not an input to the API.

Conclusion: don’t mess with their types without documentation. If of unknown purpose, they could be hit with unanticipated changes. Then, just one more thing to patch when you go to get OFF the OpenAI platform…

---

<div class="post-metadata">

**Author:** ![zzbbyy](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/zzbbyy/32/520007_2.png) [@zzbbyy](https://community.openai.com/u/zzbbyy)\
**Post date:** [April 23, 2024, 7:26am UTC](https://community.openai.com/t/a-correct-message-in-response-to-a-tool-call-cannot-validate-as-chatcompletionmessage/725036/6 "2024-04-23T07:26:53Z")

</div>

The code from the example works. I wanted to make it more robust and use and object for the message instead of a raw dictionary. I found a class that _seems_ to be designed to contain the message data structure. But alas not really - as was mentioned somewhere else in this thread - `ChatCompletionMessage` is meant to be a message **from the server** and as a message created by other means it works only sometimes. This is rather surprising.

And to answer some other points in this thread:

1. I did not try to create a `ChatCompletion` which has fields for model name, temperature etc. `ChatCompletionMessage` is a different class.
2. I still wanted to use `messages.append`, I just wanted to change the parameter of that call from a raw dictionary to an object.
