openai/openai-python · error · TypeError

Passing both `files` and `content` is not supported

Error message

Passing both `files` and `content` is not supported

What it means

Async upload_file_chunked raises this when `file` is in-memory bytes, a filename was supplied, but the `bytes` total-size argument is missing. The upload session cannot be created without a declared size, and bytes objects passed this way don't supply it, so the SDK raises TypeError.

Source

Thrown at src/openai/_base_client.py:1357

        stream_cls: type[_StreamT] | None = None,
    ) -> ResponseT | _StreamT: ...

    def post(
        self,
        path: str,
        *,
        cast_to: Type[ResponseT],
        body: Body | None = None,
        content: BinaryTypes | None = None,
        options: RequestOptions = {},
        files: RequestFiles | None = None,
        stream: bool = False,
        stream_cls: type[_StreamT] | None = None,
    ) -> ResponseT | _StreamT:
        if body is not None and content is not None:
            raise TypeError("Passing both `body` and `content` is not supported")
        if files is not None and content is not None:
            raise TypeError("Passing both `files` and `content` is not supported")
        if isinstance(body, bytes):
            warnings.warn(
                "Passing raw bytes as `body` is deprecated and will be removed in a future version. "
                "Please pass raw bytes via the `content` parameter instead.",
                DeprecationWarning,
                stacklevel=2,
            )
        opts = FinalRequestOptions.construct(
            method="post", url=path, json_data=body, content=content, files=to_httpx_files(files), **options
        )
        return cast(ResponseT, self.request(cast_to, opts, stream=stream, stream_cls=stream_cls))

    def patch(
        self,
        path: str,
        *,
        cast_to: Type[ResponseT],
        body: Body | None = None,

View on GitHub (pinned to 9917c6e28e)

Solutions

  1. Pass bytes=len(data) whenever uploading in-memory bytes
  2. Prefer the high-level file upload helpers that compute size for you
  3. Assert the size is positive before starting

Example fix

# before
await client.uploads.upload_file_chunked(data, purpose='assistants', filename='f.bin')
# after
await client.uploads.upload_file_chunked(data, bytes=len(data), purpose='assistants', filename='f.bin')
Defensive patterns

Strategy: validation

Validate before calling

assert filename and isinstance(size, int) and size >= 0, 'bytes uploads need filename and byte count'

Type guard

def chunked_args_ready(data: object, filename: object, size: object) -> bool:
    return (not isinstance(data, (bytes, bytearray))) or (bool(filename) and isinstance(size, int) and size >= 0)

Prevention

When it happens

Trigger: Awaiting upload_file_chunked with bytes, filename=..., but no bytes=len(...) argument.

Common situations: Mirroring the low-level three-step upload (create/parts/complete) manually, or copying sync example code into async without the size parameter.

Related errors


AI-assisted analysis of openai/openai-python@9917c6e28e (2026-08-28). Data as JSON: /api/errors/73d90f1081a3ded3. Report an issue: GitHub.