DataDog / dd-trace-py

Datadog Python APM Client
https://ddtrace.readthedocs.io/
Other
550 stars 412 forks source link

OpenAI completion logging fails when using function-calling #7282

Closed jaley closed 1 year ago

jaley commented 1 year ago

Summary of problem

If you have OpenAI chat completion logging enabled, and your code is using the recent ChatGPT or GPT 4 function-calling features, ddtrace will fail to log completions and there will be a lot of warnings like this:

Failed to set text tag 'openai.response.choices.0.message.content', 1 additional messages skipped
...
TypeError: not expecting type '<class 'NoneType'>'
raise TypeError("not expecting type '%s'" % type(s))
File "/venv/lib/python3.11/site-packages/six.py", line 955, in ensure_text
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
self._meta[key] = ensure_text(value, errors="replace")
File "/venv/lib/python3.11/site-packages/ddtrace/span.py", line 383, in set_tag_str
Traceback (most recent call last):

I believe this is because the code here effectively assumes that the content member on a returned message completion can never be null, but if you check the API docs here, that's actually not the case. It seems it can be null if instead function_call has been set.

Which version of dd-trace-py are you using?

1.20.3

Which version of pip are you using?

23.1.2

Which libraries and their versions are you using?

`pip freeze` aiohttp==3.8.5 aiosignal==1.3.1 alembic==1.12.0 amqp==5.1.1 anthropic==0.3.11 anyio==3.7.1 astroid==2.15.8 async-timeout==4.0.3 asyncpg==0.27.0 attrs==23.1.0 Authlib==1.2.1 azure-core==1.29.4 azure-storage-blob==12.18.2 beautifulsoup4==4.12.2 billiard==4.1.0 black==22.12.0 brotlipy==0.7.0 bytecode==0.15.0 cachetools==5.3.1 cattrs==23.1.2 celery==5.3.1 certifi==2023.7.22 cffi==1.16.0 charset-normalizer==3.3.0 click==8.1.7 click-didyoumean==0.3.0 click-plugins==1.1.1 click-repl==0.3.0 colorama==0.4.6 coverage==7.3.2 cryptography==41.0.4 databases==0.7.0 dataclasses-json==0.5.9 ddsketch==2.0.4 ddtrace==1.20.3 Deprecated==1.2.14 deprecation==2.1.0 dill==0.3.7 distro==1.8.0 docker==6.1.3 envier==0.4.0 fastapi==0.95.2 fastapi-pagination==0.12.10 filelock==3.12.4 flake8==6.1.0 flower==2.0.1 frozenlist==1.4.0 fsspec==2023.9.2 google-api-core==2.12.0 google-api-python-client==2.102.0 google-auth==2.23.2 google-auth-httplib2==0.1.1 googleapis-common-protos==1.60.0 greenlet==2.0.2 gunicorn==20.1.0 h11==0.14.0 halo==0.0.31 httpcore==0.17.3 httplib2==0.22.0 httptools==0.6.0 httpx==0.24.1 huggingface-hub==0.16.4 humanize==4.8.0 idna==3.4 importlib-metadata==6.8.0 iniconfig==2.0.0 isodate==0.6.1 isort==5.12.0 itsdangerous==2.1.2 json-logic-qubit==0.9.1 jsonschema==4.19.1 jsonschema-specifications==2023.7.1 kombu==5.3.2 langchain==0.0.287 langsmith==0.0.42 lazy-object-proxy==1.9.0 Levenshtein==0.22.0 log-symbols==0.0.14 Mako==1.2.4 MarkupSafe==2.1.3 marshmallow==3.20.1 marshmallow-enum==1.5.1 mccabe==0.7.0 multidict==6.0.4 mypy-extensions==1.0.0 number-parser==0.3.2 numexpr==2.8.7 numpy==1.25.2 openai==0.27.10 opentelemetry-api==1.20.0 orjson==3.9.7 packaging==23.2 pandas==1.5.3 parsy==1.4.0 pathspec==0.11.2 Pillow==10.0.1 platformdirs==3.11.0 pluggy==1.3.0 prometheus-client==0.17.1 prompt-toolkit==3.0.39 protobuf==4.24.4 psycopg2-binary==2.9.9 pyasn1==0.5.0 pyasn1-modules==0.3.0 pycodestyle==2.11.0 pycountry==22.3.5 pycparser==2.21 pydantic==1.10.13 pyflakes==3.1.0 pylint==2.17.7 pyparsing==3.1.1 PyPDF2==2.12.1 pytest==7.4.2 pytest-asyncio==0.20.3 pytest-cov==4.1.0 pytest-mock==3.11.1 pytest-recording==0.12.2 python-dateutil==2.8.2 python-dotenv==1.0.0 python-Levenshtein==0.22.0 python-multipart==0.0.6 pytz==2023.3.post1 PyYAML==6.0.1 rapidfuzz==3.3.1 redis==5.0.1 referencing==0.30.2 regex==2022.10.31 reportlab==3.6.13 requests==2.31.0 -e git+ssh://git@github.com/orbitalwitness/re-concierge.git@f8825f942fecd1974465ed43a63380a23bc16b04#egg=risks&subdirectory=api rpds-py==0.10.3 rsa==4.9 sentry-sdk==1.31.0 six==1.16.0 slack-sdk==3.23.0 sniffio==1.3.0 soupsieve==2.5 spinners==0.0.24 SQLAlchemy==1.4.49 SQLAlchemy-Utils==0.39.0 starlette==0.27.0 structlog==22.3.0 syrupy==4.5.0 tenacity==8.2.3 termcolor==2.3.0 testcontainers==3.7.1 testcontainers-core==0.0.1rc1 testcontainers-postgres==0.0.1rc1 testcontainers-redis==0.0.1rc1 thefuzz==0.19.0 tiktoken==0.4.0 tokenizers==0.14.0 tomlkit==0.12.1 tornado==6.3.3 tqdm==4.66.1 typer==0.7.0 types-pyOpenSSL==23.2.0.2 types-redis==4.6.0.7 typing-inspect==0.9.0 typing_extensions==4.8.0 tzdata==2023.3 uritemplate==4.1.1 urllib3==2.0.6 uvicorn==0.21.1 uvloop==0.17.0 validators==0.22.0 vcrpy==4.3.1 vine==5.0.0 watchdog==3.0.0 watchfiles==0.20.0 wcwidth==0.2.8 weaviate-client==3.24.2 websocket-client==1.6.3 websockets==11.0.3 wrapt==1.15.0 xmltodict==0.13.0 yarl==1.9.2 zipp==3.17.0

How can we reproduce your problem?

What is the result that you get?

Stack trace above in the summary

What is the result that you expected?

Completion logged, no warnings

Yun-Kim commented 1 year ago

Hi @jaley, thanks for reaching out! Apologies for the late response.

It looks like we've addressed this issue in #7054, which was released in ddtrace versions 1.20.4, 1.19.4, 2.0.1, and 2.1.0. I recommend upgrading to any of the above versions of ddtrace or newer.

I'll close this issue in the meantime but feel free to re-open if you still run into any issues, thanks!