openai / openai-python

The official Python library for the OpenAI API
https://pypi.org/project/openai/
Apache License 2.0
21.76k stars 2.98k forks source link

Allow logging request body #1522

Open palvarezcordoba opened 1 month ago

palvarezcordoba commented 1 month ago

Confirm this is a feature request for the Python library and not the underlying OpenAI API.

Describe the feature or improvement you're requesting

It would be nice to be able to be able to log the body of the requests. I needed to see what was sent over the wire, so I checked how to log body requests. Turns out, there's no way.

I needed to modify this, in order to do it. https://github.com/openai/openai-python/blob/58bec2ffe5274b37fed3adad8da188f0cbc5406c/src/openai/_base_client.py#L447-L448

Could you add support to log body requests?

I'm not sure if this is the only place that has to change. In fact, I just ended capturing network traffic just to be sure. Having to resort to this kind of technique, is not nice.

I'm aware that you can log request/response using a custom httpx client with a custom transport: https://github.com/encode/httpx/discussions/3073

EDIT: the same applies to responses body

Additional context

No response

rattrayalex commented 1 month ago

Thanks for the request, I agree this would be nice to provide! I don't expect we'll be able to get to it soon, but we do want to make logging & tracing comprehensively nicer to deal with in the coming months.

We invite anyone visiting this issue to add other use-cases they'd like to see supported with logging etc.

lmolkova commented 1 month ago

@rattrayalex we're defining GenAI/LLM conventions for distributed tracing and metrics in OpenTelemetry - https://github.com/open-telemetry/semantic-conventions/tree/main/docs/gen-ai and have a group of people working on otel and GenAI observability.

Would love to share more and collaborate, we're always available here:

| Semantic Conventions: LLM | Every Wednesday alternating between 10:00 and 16:00 PT | Google Doc | #otel-llm-semconv-wg | calendar-semconv

karthikscale3 commented 1 month ago

Thanks for the request, I agree this would be nice to provide! I don't expect we'll be able to get to it soon, but we do want to make logging & tracing comprehensively nicer to deal with in the coming months.

We invite anyone visiting this issue to add other use-cases they'd like to see supported with logging etc.

@rattrayalex - As mentioned by @lmolkova in the comment above, there is a CNCF open telemetry working group that is driving standardization of tracing across LLM vendors. We have a initial spec which we are currently iterating on and it would be great if this project adopts to the standard conventions.

RobertCraigie commented 1 month ago

@karthikscale3 @lmolkova unfortunately we will not likely be able to prioritise this soon, however I would be interested in seeing a sample of what the changes we would have to make would look like if anyone is able to share?

karthikscale3 commented 1 month ago

@karthikscale3 @lmolkova unfortunately we will not likely be able to prioritise this soon, however I would be interested in seeing a sample of what the changes we would have to make would look like if anyone is able to share?

I am happy to put something together and share it here. Give me a couple of days

kristapratico commented 1 month ago

@karthikscale3 Awesome! I'll share in case it helps - awhile back I had experimented with instrumenting the library using a decorator approach: https://github.com/kristapratico/openai-python/pull/7