Reference · Snippet · python
OpenAI chat with tools
Last updated
from openai import OpenAI
client = OpenAI()
tools = [{
"type": "function",
"function": {
"name": "get_order_status",
"description": "Look up an order by ID",
"parameters": {
"type": "object",
"properties": {"order_id": {"type": "string"}},
"required": ["order_id"],
},
},
}]
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "Status for order ORD-42?"}],
tools=tools,
)
tool_call = response.choices[0].message.tool_calls[0]
# run your handler with json.loads(tool_call.function.arguments)