Issue: no “raw” method to get headers with a Responses API call via Python SDK library with Pydantic schema passed as structured response format.
Chat Completions
response = client.beta.chat.completions.with_raw_response.parse(
model="gpt-4o-mini",
messages=[
{"role": "system", "content": "You are a helpful AI agent."},
{"role": "user", "content": text},
],
response_format=JsonResponse, # Pydantic BaseModel as schema input
)
This API call makes headers available by response.headers
, example usage:
header_dict = dict(response.headers)
print(f"Remaining: {header_dict["x-ratelimit-remaining-requests"]} requests,",
f"{header_dict["x-ratelimit-remaining-tokens"]} tokens",)
Then you can proceed to get your parsed response. And make decisions based on the ratelimit fields in headers, etc.
Responses API:
You’d like to get some headers not just for rate limits, but to see if there is a x-request-id
returned when there is an error - and I’m trying to make errors and report on them.
.create() works:
response = await client.responses.with_raw_response.create(...)
Try this though (with its SDK-only parameter for Pydantic response). Adding the with_raw_response
in the same manner fails with the .parse() method:
client = AsyncOpenAI()
response = await client.responses.with_raw_response.parse(
input=input, # A messages list
model=model,
text_format = JsonResponse, # SDK library parameter only, for BaseModel schema structured output
...
line 125, in main
response = await client.responses.with_raw_response.parse(
AttributeError: 'AsyncResponsesWithRawResponse' object has no attribute 'parse'
“Legacy” raw response results with headers:
+good: chat completions with *create*
+good: chat completions with *parse* helper
+good: responses with create
-bad: response with parse helper
Tell me I’m overlooking something…otherwise this is one more feature not delivered with parity in the Responses API.