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

ddtrace-run error using OpenAI Chat Completions API #7737

Closed
ykeremy opened this issue Nov 23, 2023 · 7 comments · Fixed by #7759
Closed

ddtrace-run error using OpenAI Chat Completions API #7737

ykeremy opened this issue Nov 23, 2023 · 7 comments · Fixed by #7759
Labels

Comments

@ykeremy
Copy link

ykeremy commented Nov 23, 2023

Summary of problem

Can't use the chat completions API using OpenAI python SDK. I tried with these models: gpt-4-1106-preview, gpt-4-vision-preview (with/without image), gpt-4.

I also tried setting these env variables but it doesn't get rid of the problem:

DD_OPENAI_LOGS_ENABLED=false
DD_OPENAI_METRICS_ENABLED=false

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

2.3.1

Which version of pip are you using?

Tried both Poetry (version 1.6.1) and pip 23.3.1

Which libraries and their versions are you using?

openai version 1.3.5

How can we reproduce your problem?

The below snippet works without ddtrace-run but gives the error shared below when I run it with ddtrace-run.

from openai import OpenAI

messages = [
    {
        "role": "user",
        "content": [
            {
                "type": "text",
                "text": "Hello, I believe I'm a bug!",
            },
        ]
    }
]

# Set your API key
client = OpenAI()
response = client.chat.completions.create(
    model="gpt-4-vision-preview",
    messages=messages,
)
print(response.choices[0].message.content)

What is the result that you get?

    response = await available_client.client.chat.completions.with_raw_response.create(**chat_completion_kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/openai/_response.py", line 261, in wrapped
    return cast(APIResponse[R], await func(*args, **kwargs))
                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/ddtrace/contrib/openai/patch.py", line 433, in patched_endpoint
    g.send(None)
  File "/usr/local/lib/python3.11/site-packages/ddtrace/contrib/openai/patch.py", line 365, in _traced_endpoint
    hook.send(None)
  File "/usr/local/lib/python3.11/site-packages/ddtrace/contrib/openai/_endpoint_hooks.py", line 69, in handle_request
    self._record_request(pin, integration, span, args, kwargs)
  File "/usr/local/lib/python3.11/site-packages/ddtrace/contrib/openai/_endpoint_hooks.py", line 254, in _record_request
    content = integration.trunc(m.get("content", ""))
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/ddtrace/contrib/_trace_utils_llm.py", line 138, in trunc
    text = text.replace("\n", "\\n").replace("\t", "\\t")
           ^^^^^^^^^^^^
AttributeError: 'list' object has no attribute 'replace'

What is the result that you expected?

No errors

@Yun-Kim
Copy link
Contributor

Yun-Kim commented Nov 27, 2023

Hi @ykeremy, thanks for reaching out!

Currently we don't yet support image inputs for the chat completions endpoint. We'll work on a fix in the meantime.

@bishoco
Copy link

bishoco commented Nov 29, 2023

I am having a similar issue using ddtrace and openai. I'm using ddtrace 2.3.1 and openai 1.3.5. If this is a separate issue, I can open up a new issue. I had to disable DataDog.

Here is the stack trace of the error:
self.llm_client.chat.completions.create( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/openai/_utils/_utils.py", line 272, in wrapper def wrapper(*args: object, **kwargs: object) -> object: File "/usr/local/lib/python3.11/site-packages/ddtrace/contrib/openai/patch.py", line 401, in patched_endpoint g.send(None) File "/usr/local/lib/python3.11/site-packages/ddtrace/contrib/openai/patch.py", line 365, in _traced_endpoint hook.send(None) File "/usr/local/lib/python3.11/site-packages/ddtrace/contrib/openai/_endpoint_hooks.py", line 69, in handle_request self._record_request(pin, integration, span, args, kwargs) File "/usr/local/lib/python3.11/site-packages/ddtrace/contrib/openai/_endpoint_hooks.py", line 254, in _record_request content = integration.trunc(m.get("content", "")) ^^^^^ File "/usr/local/lib/python3.11/site-packages/pydantic/main.py", line 755, in getattr raise AttributeError(f'{type(self).name!r} object has no attribute {item!r}') from exc AttributeError: 'ChatCompletionMessage' object has no attribute 'get'

@Yun-Kim
Copy link
Contributor

Yun-Kim commented Nov 29, 2023

Hi @bishoco, could you also include the input that you're sending to the chat completions endpoint? This would help me reproduce your error. Thanks!

@bishoco
Copy link

bishoco commented Nov 29, 2023

@Yun-Kim This is how I'm calling it:

model="gpt-4-1106-preview"
messages = [{"role": "user", "content": "Hello!"}]

stream = self.llm_client.chat.completions.create( model=model, messages=messages, stream=True )

@Yun-Kim
Copy link
Contributor

Yun-Kim commented Nov 29, 2023

@bishoco I'm not able to reproduce your error, using openai==1.3.5 and ddtrace==2.3.1 with this setup:

import openai
client = openai.OpenAI()
model = "gpt-4-1106-preview"
messages = [{"role": "user", "content": "Hello!"}]
stream = client.chat.completions.create(model=model, messages=messages, stream=True)

Do you see anything different about this than how you're calling the chat completions endpoint?

@bishoco
Copy link

bishoco commented Nov 29, 2023

@Yun-Kim I just gave you a simplified version of the messages I'm sending. The messages I'm sending are actually a bit different. Let me see if I can pull what I'm actually sending in messages to see if that reproduces the error. I'll get back to you.

Yun-Kim added a commit that referenced this issue Dec 1, 2023
Resolves #7737.

This PR adds a step to stringify input messages before tagging in the
OpenAI chat completions endpoint. Previously, we had assumed that
`messages.content` would always be a string (which was true until OpenAI
recently added the image input feature to the chat completions
endpoint), but it can now be an array of str-str dictionaries.

## Testing Strategy

Regression tests have been added, and manual testing has also confirmed
that the error reported on #7737 does not appear.

## Checklist

- [x] Change(s) are motivated and described in the PR description.
- [x] Testing strategy is described if automated tests are not included
in the PR.
- [x] Risk is outlined (performance impact, potential for breakage,
maintainability, etc).
- [x] Change is maintainable (easy to change, telemetry, documentation).
- [x] [Library release note
guidelines](https://ddtrace.readthedocs.io/en/stable/releasenotes.html)
are followed. If no release note is required, add label
`changelog/no-changelog`.
- [x] Documentation is included (in-code, generated user docs, [public
corp docs](https://github.com/DataDog/documentation/)).
- [x] Backport labels are set (if
[applicable](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting))

## Reviewer Checklist

- [ ] Title is accurate.
- [ ] No unnecessary changes are introduced.
- [ ] Description motivates each change.
- [ ] Avoids breaking
[API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces)
changes unless absolutely necessary.
- [ ] Testing strategy adequately addresses listed risk(s).
- [ ] Change is maintainable (easy to change, telemetry, documentation).
- [ ] Release note makes sense to a user of the library.
- [ ] Reviewer has explicitly acknowledged and discussed the performance
implications of this PR as reported in the benchmarks PR comment.
- [ ] Backport labels are set in a manner that is consistent with the
[release branch maintenance
policy](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)
- [ ] If this PR touches code that signs or publishes builds or
packages, or handles credentials of any kind, I've requested a review
from `@DataDog/security-design-and-guidance`.
- [ ] This PR doesn't touch any of that.
Yun-Kim added a commit that referenced this issue Dec 1, 2023
Resolves #7737.

This PR adds a step to stringify input messages before tagging in the
OpenAI chat completions endpoint. Previously, we had assumed that
`messages.content` would always be a string (which was true until OpenAI
recently added the image input feature to the chat completions
endpoint), but it can now be an array of str-str dictionaries.

Regression tests have been added, and manual testing has also confirmed
that the error reported on #7737 does not appear.

- [x] Change(s) are motivated and described in the PR description.
- [x] Testing strategy is described if automated tests are not included
in the PR.
- [x] Risk is outlined (performance impact, potential for breakage,
maintainability, etc).
- [x] Change is maintainable (easy to change, telemetry, documentation).
- [x] [Library release note
guidelines](https://ddtrace.readthedocs.io/en/stable/releasenotes.html)
are followed. If no release note is required, add label
`changelog/no-changelog`.
- [x] Documentation is included (in-code, generated user docs, [public
corp docs](https://github.com/DataDog/documentation/)).
- [x] Backport labels are set (if
[applicable](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting))

- [ ] Title is accurate.
- [ ] No unnecessary changes are introduced.
- [ ] Description motivates each change.
- [ ] Avoids breaking
[API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces)
changes unless absolutely necessary.
- [ ] Testing strategy adequately addresses listed risk(s).
- [ ] Change is maintainable (easy to change, telemetry, documentation).
- [ ] Release note makes sense to a user of the library.
- [ ] Reviewer has explicitly acknowledged and discussed the performance
implications of this PR as reported in the benchmarks PR comment.
- [ ] Backport labels are set in a manner that is consistent with the
[release branch maintenance
policy](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)
- [ ] If this PR touches code that signs or publishes builds or
packages, or handles credentials of any kind, I've requested a review
from `@DataDog/security-design-and-guidance`.
- [ ] This PR doesn't touch any of that.
Yun-Kim added a commit that referenced this issue Dec 1, 2023
Resolves #7737.

This PR adds a step to stringify input messages before tagging in the
OpenAI chat completions endpoint. Previously, we had assumed that
`messages.content` would always be a string (which was true until OpenAI
recently added the image input feature to the chat completions
endpoint), but it can now be an array of str-str dictionaries.

Regression tests have been added, and manual testing has also confirmed
that the error reported on #7737 does not appear.

- [x] Change(s) are motivated and described in the PR description.
- [x] Testing strategy is described if automated tests are not included
in the PR.
- [x] Risk is outlined (performance impact, potential for breakage,
maintainability, etc).
- [x] Change is maintainable (easy to change, telemetry, documentation).
- [x] [Library release note
guidelines](https://ddtrace.readthedocs.io/en/stable/releasenotes.html)
are followed. If no release note is required, add label
`changelog/no-changelog`.
- [x] Documentation is included (in-code, generated user docs, [public
corp docs](https://github.com/DataDog/documentation/)).
- [x] Backport labels are set (if
[applicable](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting))

- [ ] Title is accurate.
- [ ] No unnecessary changes are introduced.
- [ ] Description motivates each change.
- [ ] Avoids breaking
[API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces)
changes unless absolutely necessary.
- [ ] Testing strategy adequately addresses listed risk(s).
- [ ] Change is maintainable (easy to change, telemetry, documentation).
- [ ] Release note makes sense to a user of the library.
- [ ] Reviewer has explicitly acknowledged and discussed the performance
implications of this PR as reported in the benchmarks PR comment.
- [ ] Backport labels are set in a manner that is consistent with the
[release branch maintenance
policy](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)
- [ ] If this PR touches code that signs or publishes builds or
packages, or handles credentials of any kind, I've requested a review
from `@DataDog/security-design-and-guidance`.
- [ ] This PR doesn't touch any of that.
@Yun-Kim
Copy link
Contributor

Yun-Kim commented Dec 1, 2023

Hi @bishoco, please create a new issue if you're still seeing the error, it is most likely separate from this issue as we've fixed this in #7759.

majorgreys pushed a commit that referenced this issue Dec 4, 2023
…#7759 to 2.2] (#7813)

This backports #7759 to 2.2.

This PR adds a step to stringify input messages before tagging in the
OpenAI chat completions endpoint. Previously, we had assumed that
messages.content would always be a string (which was true until OpenAI
recently added the image input feature to the chat completions
endpoint), but it can now be an array of str-str dictionaries.

## Testing Strategy

Regression tests have been added, and manual testing has also confirmed
that the error reported on #7737 does not appear.

## Checklist

- [x] Change(s) are motivated and described in the PR description.
- [x] Testing strategy is described if automated tests are not included
in the PR.
- [x] Risk is outlined (performance impact, potential for breakage,
maintainability, etc).
- [x] Change is maintainable (easy to change, telemetry, documentation).
- [x] [Library release note
guidelines](https://ddtrace.readthedocs.io/en/stable/releasenotes.html)
are followed. If no release note is required, add label
`changelog/no-changelog`.
- [x] Documentation is included (in-code, generated user docs, [public
corp docs](https://github.com/DataDog/documentation/)).
- [x] Backport labels are set (if
[applicable](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting))

## Reviewer Checklist

- [x] Title is accurate.
- [x] No unnecessary changes are introduced.
- [x] Description motivates each change.
- [x] Avoids breaking
[API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces)
changes unless absolutely necessary.
- [x] Testing strategy adequately addresses listed risk(s).
- [x] Change is maintainable (easy to change, telemetry, documentation).
- [x] Release note makes sense to a user of the library.
- [x] Reviewer has explicitly acknowledged and discussed the performance
implications of this PR as reported in the benchmarks PR comment.
- [x] Backport labels are set in a manner that is consistent with the
[release branch maintenance
policy](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)
- [x] If this PR touches code that signs or publishes builds or
packages, or handles credentials of any kind, I've requested a review
from `@DataDog/security-design-and-guidance`.
- [x] This PR doesn't touch any of that.
majorgreys pushed a commit that referenced this issue Dec 4, 2023
…#7759 to 2.3] (#7814)

This backports #7759 to 2.3.

This PR adds a step to stringify input messages before tagging in the
OpenAI chat completions endpoint. Previously, we had assumed that
messages.content would always be a string (which was true until OpenAI
recently added the image input feature to the chat completions
endpoint), but it can now be an array of str-str dictionaries.

## Testing Strategy

Regression tests have been added, and manual testing has also confirmed
that the error reported on #7737 does not appear.

## Checklist

- [x] Change(s) are motivated and described in the PR description.
- [x] Testing strategy is described if automated tests are not included
in the PR.
- [x] Risk is outlined (performance impact, potential for breakage,
maintainability, etc).
- [x] Change is maintainable (easy to change, telemetry, documentation).
- [x] [Library release note
guidelines](https://ddtrace.readthedocs.io/en/stable/releasenotes.html)
are followed. If no release note is required, add label
`changelog/no-changelog`.
- [x] Documentation is included (in-code, generated user docs, [public
corp docs](https://github.com/DataDog/documentation/)).
- [x] Backport labels are set (if
[applicable](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting))

## Reviewer Checklist

- [x] Title is accurate.
- [x] No unnecessary changes are introduced.
- [x] Description motivates each change.
- [x] Avoids breaking
[API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces)
changes unless absolutely necessary.
- [x] Testing strategy adequately addresses listed risk(s).
- [x] Change is maintainable (easy to change, telemetry, documentation).
- [x] Release note makes sense to a user of the library.
- [x] Reviewer has explicitly acknowledged and discussed the performance
implications of this PR as reported in the benchmarks PR comment.
- [x] Backport labels are set in a manner that is consistent with the
[release branch maintenance
policy](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)
- [x] If this PR touches code that signs or publishes builds or
packages, or handles credentials of any kind, I've requested a review
from `@DataDog/security-design-and-guidance`.
- [x] This PR doesn't touch any of that.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants