0Pricing
AI Agents · Lesson

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

  1. How Function Calling Works
  2. Defining Tool Schemas (JSON Schema)
  3. Choosing Tools at Runtime
  4. Returning Results to the Model
← Back to AI Agents