Returning Results to the Model
Format tool outputs as tool-role messages so the model can read them and continue the conversation.
The Tool Result Message
After running a tool, you tell the model the outcome with a tool message:
messages.append({
'role': 'tool',
'tool_call_id': tool_call.id, # MUST match the assistant's tool_call.id
'content': json.dumps(result)
})Content Should Be a String
OpenAI expects content to be a string. Serialize objects with json.dumps:
# Bad — will error
messages.append({'role': 'tool', 'tool_call_id': id, 'content': {'temp': 18}})
# Good
messages.append({'role': 'tool', 'tool_call_id': id, 'content': '{"temp": 18}'})All lessons in this course
- How Function Calling Works
- Defining Tool Schemas (JSON Schema)
- Choosing Tools at Runtime
- Returning Results to the Model