openai-python icon indicating copy to clipboard operation
openai-python copied to clipboard

Responses create spends ~28ms of CPU per request transforming a body it returns unchanged

Open zdurm opened this issue 1 month ago • 1 comments

Confirm this is an issue with the Python library and not an underlying OpenAI API

  • [x] This is an issue with the Python library

Describe the bug

AsyncResponses.create runs async_maybe_transform over the whole request body before every request. With a long input and many tools, the transform takes about 28ms of CPU on the event loop per request. For a body of plain dicts (no aliased keys, no format fields, no file inputs) the transform returns a body equal to its input, so that CPU time does not change the request.

In an asyncio app that shares the event loop with latency-sensitive work, such as streaming audio, each request blocks that work for the whole transform. The cost grows linearly with the conversation length, because the full history is sent on every turn.

Responses.create (sync) runs the same transform.

To Reproduce

  1. Build a Responses request body with about 180 input items and 86 function tools.
  2. Time async_maybe_transform(body, ResponseCreateParamsStreaming), the call that AsyncResponses.create makes.
  3. Compare the output with the input.

The script below does this with synthetic data and no network.

Code snippets

import asyncio, time
from openai._utils import async_maybe_transform
from openai.types.responses import response_create_params as p

def body(n_items=136, n_tools=86):
    items = []
    for i in range(n_items):
        if i % 3 == 2:
            items.append({"type": "function_call", "call_id": f"call_{i}", "name": "lookup", "arguments": '{"q": "x"}'})
            items.append({"type": "function_call_output", "call_id": f"call_{i}", "output": "result " * 200})
        else:
            items.append({"role": "user" if i % 2 else "assistant", "content": [{"type": "input_text" if i % 2 else "output_text", "text": "lorem ipsum " * 100}]})
    tools = [{"type": "function", "name": f"tool_{i}", "description": "does a thing " * 10, "strict": False,
              "parameters": {"type": "object", "properties": {f"arg_{j}": {"type": "string", "description": "an argument"} for j in range(8)}, "required": ["arg_0"]}}
             for i in range(n_tools)]
    return {"model": "gpt-4.1", "input": items, "tools": tools, "stream": True}

async def main():
    b = body()
    for _ in range(3):
        await async_maybe_transform(b, p.ResponseCreateParamsStreaming)
    runs = []
    for _ in range(10):
        t = time.perf_counter(); out = await async_maybe_transform(b, p.ResponseCreateParamsStreaming); runs.append((time.perf_counter() - t) * 1000)
    print(f"items={len(b['input'])} tools={len(b['tools'])} transform ms: min={min(runs):.1f} median={sorted(runs)[5]:.1f}")
    print("output == input:", out == b)

asyncio.run(main())

Output:

items=181 tools=86 transform ms: min=28.0 median=28.4
output == input: True

A possible direction: decide once per type, and cache, whether its annotations contain any PropertyInfo (alias, format, discriminator). When a type contains none, return the data without walking it.

OS

macOS 26.5

Python version

Python 3.14.3

Library version

openai 3.24.0

zdurm avatar Oct 02 '26 23:10 zdurm

Duplicate of #4021, which I filed through the issue form.

zdurm avatar Oct 02 '26 23:10 zdurm