Here the documentation shows how an optional property can be emulated with Structured Outputs using a union type with null.
However, I can’t get this to work as expected. Below is an example adapted from the example in the documentation. I have set ‘unit’ property type to ‘[“string”, “null”]’, but despite what I put in the prompt, I can’t elicit a response with ‘“unit”:null’. If I turn off Structured Outputs by setting ‘“strict”:False’, I do get ‘“unit”:null’ with the example below. I am using gpt-4o-2024-08-06, api version 2024-10-21, openai-python 1.57.4
Am I doing something wrong, or is this a potential bug?
Example Code:
messages=[
{"role": "system", "content": "You are a helpful assistant. If you are asked about weather information, extract the desired location and temperature unit in the provided get_weather format. If the desired temperature unit is not explicitly given by the user, return null for that property"},
{"role": "user", "content": "Give me the temperature in Portland"}
]
json_schema = {
"name": "get_weather",
"description": "Fetches the weather in the given location",
"strict": True,
"schema": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The location to get the weather for"
},
"unit": {
"type": ["string", "null"],
"description": "The unit to return the temperature in",
"enum": ["F", "C"]
}
},
"additionalProperties": False,
"required": [
"location", "unit"
]
}
}
response = client.chat.completions.create(
model=deployment,
messages=messages,
response_format={
"type": "json_schema",
"json_schema": json_schema
}
)
print(json.dumps(json.loads(response.choices[0].message.content), indent=2))
Example Output:
{
"location": "Portland",
"unit": "C"
}