In response to a tool call we need to create the following message:
{
"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 under Example invoking multiple function calls in one response and this seems to work.
But this structure does not validate as a ChatCompletionMessage:
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:
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.