Loading...
Loading...
Get reliable, parseable outputs from LLMs — JSON mode, function calling, and schema validation
Force the model to return valid JSON:
response = client.chat.completions.create(
model='gpt-4o',
messages=[
{'role': 'system', 'content': 'Extract information as JSON.'},
{'role': 'user', 'content': 'Alice bought 3 items for $45.'}
],
response_format={'type': 'json_object'}
)Define tools the model can call:
tools = [{
'type': 'function',
'function': {
'name': 'get_weather',
'description': 'Get weather for a location',
'parameters': {
'type': 'object',
'properties': {
'location': {'type': 'string'},
'units': {'type': 'string', 'enum': ['celsius', 'fahrenheit']}
},
'required': ['location']
}
}
}]The model requests tool calls; your code executes them and returns results.
import json
try:
data = json.loads(response.choices[0].message.content)
except json.JSONDecodeError:
print("Invalid JSON — retry or fall back")Define a tool for searching products:
tool = {
'type': 'function',
'function': {
'name': 'search_products',
'description': 'Search products by keyword and price range',
'parameters': {
'type': 'object',
'properties': {
'query': {'type': 'string'},
'max_price': {'type': 'number'}
},
'required': ['query']
}
}
}