Structured output with pydantic and the Batch API

Hi everyone,

I’m getting errors while trying to use structured format and the batch api. It looks like this :

{"id": "batch_req_679ba2349f3481909d7d10bd3b8cb82a", "custom_id": "es7cuns533wid6k7dgfi87_6", "response": {"status_code": 400, "request_id": "4a9f1b44c989fac460590df5cced70a7", "body": {"error": {"message": "Missing required parameter: 'response_format.json_schema'.", "type": "invalid_request_error", "param": "response_format.json_schema", "code": "missing_required_parameter"}}}, "error": null}

This is the approach I’m running with, but obviously its not working:


  class Individual(BaseModel):
                year : int
                unite : str
                kpi : str
                value : str
                found : str

  class MasterCase(BaseModel):
                extraction : list[Individual]
             

 task = {
                 "custom_id": f"{customid}",
                 "method": "POST",
                 "url": "/v1/chat/completions",
                 "body":{
                     "model": "gpt-4o",
                     "temperature": 0.2,
                     "top_p": 0.1,
                     "response_format":
                         {"type": "json_schema",
                         "schema":to_strict_json_schema(MasterCase)},
                     "messages": [
                           {"role": "system", "content": "You are an helpful assistant."},
                           {"role":"user", "content" : f"{self.question_gpt}"}
                          ],
                     }
                    }

Does anyone know where the issue might be ?

Welcome to the community @DS_user

Have you checked if the requests are properly formed in the batch file that you’re submitting? Especially the response_format.json_schema param.

This is my current response format, it looks correct to me but I must be missing something since it’s not working… :

{
  "response_format": {
    "type": "json_schema",
    "schema": {
      "$defs": {
        "Individual": {
          "title": "Individual",
          "type": "object",
          "properties": {
            "year": {
              "title": "Year",
              "type": "integer"
            },
            "unite": {
              "title": "Unite",
              "type": "string"
            },
            "kpi": {
              "title": "Kpi",
              "type": "string"
            },
            "value": {
              "title": "Value",
              "type": "number"
            },
            "found": {
              "title": "Found",
              "type": "string"
            }
          },
          "required": [
            "year",
            "unite",
            "kpi",
            "value",
            "found"
          ],
          "additionalProperties": false
        }
      },
      "properties": {
        "extraction": {
          "title": "Extraction",
          "type": "array",
          "items": {
            "$ref": "#/$defs/Individual"
          }
        }
      },
      "required": [
        "extraction"
      ],
      "title": "MasterCase",
      "type": "object",
      "additionalProperties": false
    }
  }
}