Structured Outputs not working with Assistants

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.