Skip to content

images.edit() fails with Zod validation error — multipart n parameter sent as string #50

@keu

Description

@keu

When using the OpenAI Python SDK with the orq.ai proxy (base_url="https://api.orq.ai/v2/router"), calls to images.edit() fail with a 400 error because the n parameter is
received as a string instead of a number.

This happens because images.edit() uploads files, so the OpenAI SDK sends the request as multipart/form-data. Per the HTTP spec, all fields in multipart form data are encoded
as strings
. OpenAI's API handles this correctly by coercing "1"1, but the orq.ai proxy runs strict Zod validation before forwarding and rejects the string value.

images.generate() is not affected because it sends a JSON body where n is a proper integer.

Error

{
  "success": false,
  "error": {
    "name": "ZodError",
    "message": [
      {
        "expected": "number",
        "code": "invalid_type",
        "path": ["n"],
        "message": "Invalid input: expected number, received string"
      }
    ]
  }
}

Reproduction

  from openai import AsyncOpenAI

  client = AsyncOpenAI(
      base_url="https://api.orq.ai/v2/router",
      api_key="<ORQ_API_KEY>",
  )

  # ✅ This WORKS (JSON body)
  await client.images.generate(
      model="openai/gpt-image-1.5",
      prompt="A red circle",
      n=1,
      size="1024x1024",
  )

  # ❌ This FAILS (multipart/form-data body)
  await client.images.edit(
      model="openai/gpt-image-1.5",
      image=open("test.png", "rb"),
      prompt="Make this brighter",
      n=1,              # <-- sent as string "1" in multipart, rejected by Zod
      size="1024x1024",
  )

Expected behavior

The proxy should coerce numeric string values from multipart form fields to numbers before validation, matching OpenAI's API behavior.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions