Error using response_format for json-output?

Hello - i try to read a txt-file, analyse some data and ouput the result in json-format using the following code:

import os, sys
from urllib.parse import urlparse
import time
from dotenv import load_dotenv
from openai import OpenAI
from pydantic import BaseModel
from typing import List

class ArticleSummary(BaseModel):
    category: str
class Messages(BaseModel):
    messages: List[ArticleSummary]

path = os.path.abspath(os.path.dirname(sys.argv[0])) 

fn = os.path.join(path, ".env")
load_dotenv(fn)
CHATGPT_API_KEY = os.environ.get("CHATGPT_API_KEY")
client = OpenAI(api_key = CHATGPT_API_KEY)

fn = os.path.join(path, "prompt.txt")
with open(fn, encoding="utf-8", errors="ignore") as f:
  lines = f.read().splitlines()
question = "\n".join(lines)

### upload txt-file for analysis
fn = os.path.join(path, "categories.txt")
vector_store = client.vector_stores.create(name="File")
file_paths = [fn]
file_streams = [open(path, "rb") for path in file_paths]
file_batch = client.vector_stores.file_batches.upload_and_poll(
  vector_store_id=vector_store.id, files=file_streams
)
print(file_batch.status)
print(file_batch.file_counts)
# exit()    

resp = client.responses.create(
  model="gpt-4o-mini",
  input=[{"role": "user", "content": question}],
  tools=[{"type": "file_search", "vector_store_ids": [vector_store.id]}],
  response_format={
      "type": "json_schema",
      "json_schema": {
          "name": "test_schema",
          "schema": Messages.model_json_schema()
      }
  }    
)

resultAnswer = resp.output[-1].content[0].text
fnResult = os.path.join(path, "result.txt")
with open(fnResult, "w", encoding="UTF-8", errors="ignore") as f:
  f.write(resultAnswer)
print(f"Result written to {fnResult}")

print(f"Program {os.path.basename(__file__)} finished - will close soon...") 
time.sleep(5)  

Unfortunately i get this erroor when running the program:

TypeError: Responses.create() got an unexpected keyword argument ‘response_format’

How can i output the result as json-file?

You have several missteps there in forming the correct API call.

This forum post has discussion about using the server-side chat history, but the code snippet at the bottom is a fully-realized use of responses.parse() for sending in a pydantic basemodel as a text.format and the API SDK library validating and loading the output to a “parsed” alongside “content”. You can still consume the API SDK helper’s response.output_text to receive and save the plain text of the JSON response.

Thanks for your response - so when i understand this correct -

in can simply forget all this stuff from the past
response_format={
"type": "json_schema",
"json_schema": {
"name": "test_schema",
"schema": Messages.model_json_schema()
}
}

And also all the pydantic stuff i used before like:

class ArticleSummary(BaseModel)
  businessName: str
  dba: str
  ownerName: str
  ownershipPercent: str

class Messages(BaseModel)
  messages: List[ArticleSummary]

And simply define the json-strucurte i want in the prompt
and get the json data output using
resp.output_text

Correct?

You historically always could ask for a JSON and then have mixed results in receiving it depending on the quality of the prompt messages, with the typical symptom of the AI also wanting to chat about it.

Since mid-2023 models, you also could write that instruction about always producing JSON and turn on text.format → “type”: “json_object” and get some more training in producing JSON. With the Responses API correct parameter:

text={"format": { "type": "json_object"} }

However the recommended way is to use “strict” JSON structured outputs.

text={ "format": {"type": "json_schema", "strict": true, "name":"api_output", "schema": … } }

Movie review JSON AI example, including a JSON schema, a definition of the JSON output to receive:

from openai import OpenAI
client = OpenAI()

response = client.responses.create(
  model="gpt-4.1",
  input=[
    {
      "role": "system",
      "content": [
        {
          "type": "input_text",
          "text": "Your a movie reviewer. Using your knowledge output a movie score for the movie the user discusses with an IMDB-style rating of the movie."
        }
      ]
    },
    {
      "role": "user",
      "content": [
        {
          "type": "input_text",
          "text": "\"Wayne's World 2\" Movie score?"
        }
      ]
    }
  ],
  text={
    "format": {
      "type": "json_schema",
      "name": "movie_review",
      "strict": True,
      "schema": {
        "type": "object",
        "properties": {
          "movie_title": {
            "type": "string",
            "description": "The title of the movie being reviewed."
          },
          "movie_score": {
            "type": "number",
            "description": "The review score given to the movie (1-10).",
            "minimum": 1,
            "maximum": 10
          }
        },
        "required": [
          "movie_title",
          "movie_score"
        ],
        "additionalProperties": False
      }
    }
  },
  max_output_tokens=1000,
  top_p=0.01,
  store=False,
)

JSON schema is a specific subset of supported JSON keywords and features.

With the API SDK library, “response.output_text” is a collector of just the AI’s response output, without other event items seen in “output”.


The prior reply’s linked code is the opposite, not merely asking nicely, but using the OpenAI library’s method of ingesting a Pydantic BaseModel to create the strict schema, using a custom API parameter.