Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can we access request headers (OpenAI) ? #511

Open
kantandane opened this issue Dec 20, 2024 · 5 comments
Open

Can we access request headers (OpenAI) ? #511

kantandane opened this issue Dec 20, 2024 · 5 comments
Labels
question Further information is requested

Comments

@kantandane
Copy link

Hi, im wondering if pydantic-ai allows to access request response headers ?

OpenAI for example provides informations in headers like so:

Field Sample Value Description
x-ratelimit-limit-requests 60 The maximum number of requests that are permitted before exhausting the rate limit.
x-ratelimit-limit-tokens 150000 The maximum number of tokens that are permitted before exhausting the rate limit.
x-ratelimit-remaining-requests 59 The remaining number of requests that are permitted before exhausting the rate limit.
x-ratelimit-remaining-tokens 149984 The remaining number of tokens that are permitted before exhausting the rate limit.
x-ratelimit-reset-requests 1s The time until the rate limit (based on requests) resets to its initial state.
x-ratelimit-reset-tokens 6m0s The time until the rate limit (based on tokens) resets to its initial state.

Which I absolutely need in my app to be able to temporarily disable the Agent if we get close to the limits.

Is there a way to get this information ?

Thank you !

@samuelcolvin
Copy link
Member

Sorry, are you asking to be able to set request headers or read response headers, or both?

@samuelcolvin samuelcolvin added the question Further information is requested label Dec 20, 2024
@YanSte
Copy link
Contributor

YanSte commented Dec 21, 2024

I think you can have a look with OpenAIModel from Pydantic Ai.

That will solve your issue.

@kantandane
Copy link
Author

@samuelcolvin I want to be able to READ response headers.
For each request, OpenaAI sends back in the response headers my account limit status, this is what I want to be able to access when using Pydantic-Ai.

@YanSte Thank you, in OpenAIModel documentation I see we can provide our own existing http_client, so I guess this is the way to do it !

@samuelcolvin
Copy link
Member

If you want to observe the headers (and not use them in your application code), you can use the httpx instrumentation in logfire - https://logfire.pydantic.dev/docs/integrations/http-clients/httpx/

If you want to use the values at runtime, pass your own httpx client as @YanSte suggests.

@kantandane
Copy link
Author

Thank you to both of you.

I succeeded by following your advice: I created a custom AsyncClient class that I use as the http_client of my OpenAIModel.
I now have all I need to do what I want to.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants