Using the python lib with structure outputs,
is there a way to pass the json schema, rather than a pydantic format?
(as I need to generate the schema dynamically)
Passing from:
from pydantic import BaseModel
from openai import OpenAI
doc = "Why did the chicken cross the road? To get to the other side. haha."
class Info(BaseModel):
title: str
tags: list[str]
completion = client.beta.chat.completions.parse(
model="gpt-4o",
messages=[
{"role": "system", "content": "Extract the title and tags"},
{"role": "user", "content": doc},
],
response_format=MathResponse,
)
message = completion.choices[0].message
print(message.parsed.title, message.parsed.tags)
to something like this:
response_format = {
"properties": {
"title": {
"title": "Title",
"type": "string"
},
"tags": {
"items": {
"type": "string"
},
"title": "Tags",
"type": "array"
}
},
"required": [
"title",
"tags"
],
"title": "Info",
"type": "object"
}
completion = client.beta.chat.completions.parse(
model="gpt-4o",
messages=[
{"role": "system", "content": "Extract title and tags"},
{"role": "user", "content": doc},
],
response_format=json.dumps(response_format), # << what's the right way here?
)
message = completion.choices[0].message
print(message['title'], message['tags'])