Structured Outputs not working with Assistants

I’ve been trying to get the structured outputs working with the Assistants API / Python SDK with file_search enabled but getting a variety of errors - I’ve had no problem using the exact same logic with Chat Completions.

    assistant = self.client.beta.assistants.create(
                name=file.filename,
                instructions='You are an AI assistant that helps Commercial Real Estate professionals find data inside Property Brochures',
                model='gpt-4o-mini',
                tools=[{"type": "file_search"}, openai.pydantic_function_tool(<PydanticModel>)],
                )

this returns the error
openai.BadRequestError: Error code: 400 - {'error': {'message': "Unknown parameter: 'tools[1].function.strict'.", 'type': 'invalid_request_error', 'param': 'tools[1].function.strict', 'code': 'unknown_parameter'}}

i’ve also tried using response_format but get results saying this needs to be ‘json_object’ or ‘text’.

I’m using version openai v1.51.2 (wrapped in using AzureOpenAI).

Any similar issues/solutions?

Let’s try that out. Note the need for importing the full openai module.

>>>import openai, pydantic
>>>class ToolExample(pydantic.BaseModel):
...    property1: str
...    property1: int
>>>openai.pydantic_function_tool(ToolExample)
{'type': 'function', 'function': {'name': 'ToolExample', 'strict': True, 'parameters': {'properties': {'property1': {'title': 'Property1', 'type': 'integer'}}, 'required': ['property1'], 'title': 'ToolExample', 'type': 'object', 'additionalProperties': False}}}

So this method will create a ChatCompletionToolParam object.

Employing it to create an assistant:

>>>tool_obj = openai.pydantic_function_tool(ToolExample)
>>>client = openai.Client()
>>>assistant = client.beta.assistants.create(
...    instructions="You are Humorbot 3000.",
...    name="HumorBot",
...    tools=[{"type": "code_interpreter"}, tool_obj],
...    model="gpt-4o-mini",
...)

No problems. What do we get for tools in the return object?

>>>assistant.tools
[CodeInterpreterTool(type=‘code_interpreter’), FunctionTool(function=FunctionDefinition(name=‘ToolExample’, description=None, parameters={‘properties’: {‘property1’: {‘title’: ‘Property1’, ‘type’: ‘integer’}}, ‘required’: [‘property1’], ‘title’: ‘ToolExample’, ‘type’: ‘object’, ‘additionalProperties’: False}, strict=True), type=‘function’)]

Looks like it’s ready to go.

This was tested against model aliases gpt-4o and gpt-4o-mini — the only ones that support structured output for functions. Also gpt-4-turbo allows this to be set.

I would look at the imports and usage of openai and pydantic BaseModel. Try with such a simple schema before going extreme.

Then specify the latest Azure openai API version if this does work against OpenAI itself, and if Azure is known to support structured outputs. You show “strict” not validating.