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

Add Request Header Exlude Option For Debug logging

Open case-k-git opened this issue 1 year ago • 1 comments

  • [x] I understand that this repository is auto-generated and my pull request may not be merged

Changes being requested

fix this issue https://github.com/openai/openai-python/issues/1082 https://github.com/openai/openai-python/issues/1196

Sample code to reproduce

$ export OPENAI_LOG=debug
from openai import OpenAI
import os

client = OpenAI()
response = client.chat.completions.with_raw_response.create(
    messages=[{
        "role": "user",
        "content": "Say this is a test",
    }],
    model="gpt-3.5-turbo",
    extra_headers={"Authorization": f'Bearer {os.environ.get("OPENAI_API_KEY")}'},
)
print(response)
poetry run python openai_lib/sample3.py
[2024-03-13 19:23:39 - httpx:80 - DEBUG] load_ssl_context verify=True cert=None trust_env=True http2=False
[2024-03-13 19:23:39 - httpx:146 - DEBUG] load_verify_locations cafile='/Users/keisuke.taniguchi/Library/Caches/pypoetry/virtualenvs/openai-lib-wrcub86L-py3.11/lib/python3.11/site-packages/certifi/cacert.pem'
[2024-03-13 19:23:39 - openai._base_client:435 - DEBUG] Request options: {'method': 'post', 'url': '/chat/completions', 'headers': {'Authorization': 'Bearer <api-key>', 'X-Stainless-Raw-Response': 'true'}, 'files': None, 'json_data': {'messages': [{'role': 'user', 'content': 'Say this is a test'}], 'model': 'gpt-3.5-turbo'}}
[2024-03-13 19:23:39 - httpx:1026 - INFO] HTTP Request: POST https://api.openai.com/v1/chat/completions "HTTP/1.1 200 OK"
[2024-03-13 19:23:39 - openai._base_client:887 - DEBUG] HTTP Request: POST https://api.openai.com/v1/chat/completions "200 OK"
<APIResponse [200 OK] type=<class 'openai.types.chat.chat_completion.ChatCompletion'>>

Additional context & links

https://github.com/openai/openai-python/blob/main/src/openai/_models.py#L398 https://github.com/openai/openai-python/blob/main/tests/test_models.py#L510

case-k-git avatar Mar 13 '24 10:03 case-k-git

Hi guys, could this be reviewed any time soon? Also, I tried to add a filter to the openai logger and whatever I do the openai logger seems to be ignoring the applied filter/new handler. Would could be the reason?

jkondek1 avatar Oct 21 '24 13:10 jkondek1

thanks for the PR and sorry for the delay, this was fixed in https://github.com/openai/openai-python/pull/1850

RobertCraigie avatar Nov 12 '24 07:11 RobertCraigie