Skip to content

chore(llmobs): dac strip io from OpenAI #13791

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

Open
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

jsimpher
Copy link
Contributor

@jsimpher jsimpher commented Jun 26, 2025

Remove potentially sensitive i/o data from apm spans. This way, prompt and completion data will only appear on the llm obs spans, which are/will be subject to data access controls.

Mostly, this just removes io tag sets. A few things (mostly metrics) have llmobs tags dependent on span tags, so there is a bit more refactoring there.

Let me know if I removed anything that should really stay, or if I missed something that should be restricted.

This one does a lot that the others don't. I've left things like audio transcript and image/file retrieval that we don't duplicate.

Checklist

  • PR author has checked that all the criteria below are met
  • The PR description includes an overview of the change
  • The PR description articulates the motivation for the change
  • The change includes tests OR the PR description describes a testing strategy
  • The PR description notes risks associated with the change, if any
  • Newly-added code is easy to change
  • The change follows the library release note guidelines
  • The change includes or references documentation updates if necessary
  • Backport labels are set (if applicable)

Reviewer Checklist

  • Reviewer has checked that all the criteria below are met
  • Title is accurate
  • All changes are related to the pull request's stated goal
  • Avoids breaking API changes
  • Testing strategy adequately addresses listed risks
  • Newly-added code is easy to change
  • Release note makes sense to a user of the library
  • If necessary, author has 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

Copy link
Contributor

CODEOWNERS have been resolved as:

releasenotes/notes/remove-io-data-from-apm-span-openai-integration-81f3ae914a5d2faf.yaml  @DataDog/apm-python
ddtrace/contrib/internal/openai/_endpoint_hooks.py                      @DataDog/ml-observability
ddtrace/contrib/internal/openai/utils.py                                @DataDog/ml-observability
ddtrace/llmobs/_integrations/openai.py                                  @DataDog/ml-observability
tests/contrib/openai/test_openai_v1.py                                  @DataDog/ml-observability
tests/snapshots/tests.contrib.openai.test_openai.test_acompletion.json  @DataDog/ml-observability
tests/snapshots/tests.contrib.openai.test_openai.test_azure_openai_chat_completion.json  @DataDog/ml-observability
tests/snapshots/tests.contrib.openai.test_openai.test_azure_openai_completion.json  @DataDog/ml-observability
tests/snapshots/tests.contrib.openai.test_openai.test_azure_openai_embedding.json  @DataDog/ml-observability
tests/snapshots/tests.contrib.openai.test_openai.test_chat_completion.json  @DataDog/ml-observability
tests/snapshots/tests.contrib.openai.test_openai.test_chat_completion_function_calling.json  @DataDog/ml-observability
tests/snapshots/tests.contrib.openai.test_openai.test_chat_completion_image_input.json  @DataDog/ml-observability
tests/snapshots/tests.contrib.openai.test_openai.test_chat_completion_stream.json  @DataDog/ml-observability
tests/snapshots/tests.contrib.openai.test_openai.test_completion.json   @DataDog/ml-observability
tests/snapshots/tests.contrib.openai.test_openai.test_completion_stream.json  @DataDog/ml-observability
tests/snapshots/tests.contrib.openai.test_openai.test_create_moderation.json  @DataDog/ml-observability
tests/snapshots/tests.contrib.openai.test_openai.test_embedding.json    @DataDog/ml-observability
tests/snapshots/tests.contrib.openai.test_openai.test_embedding_array_of_token_arrays.json  @DataDog/ml-observability
tests/snapshots/tests.contrib.openai.test_openai.test_embedding_string_array.json  @DataDog/ml-observability
tests/snapshots/tests.contrib.openai.test_openai.test_embedding_token_array.json  @DataDog/ml-observability
tests/snapshots/tests.contrib.openai.test_openai.test_file_create.json  @DataDog/ml-observability
tests/snapshots/tests.contrib.openai.test_openai.test_file_delete.json  @DataDog/ml-observability
tests/snapshots/tests.contrib.openai.test_openai.test_file_download.json  @DataDog/ml-observability
tests/snapshots/tests.contrib.openai.test_openai.test_file_list.json    @DataDog/ml-observability
tests/snapshots/tests.contrib.openai.test_openai.test_file_retrieve.json  @DataDog/ml-observability
tests/snapshots/tests.contrib.openai.test_openai.test_image_b64_json_response.json  @DataDog/ml-observability
tests/snapshots/tests.contrib.openai.test_openai.test_image_create.json  @DataDog/ml-observability
tests/snapshots/tests.contrib.openai.test_openai.test_misuse.json       @DataDog/ml-observability
tests/snapshots/tests.contrib.openai.test_openai.test_model_delete.json  @DataDog/ml-observability
tests/snapshots/tests.contrib.openai.test_openai.test_model_list.json   @DataDog/ml-observability
tests/snapshots/tests.contrib.openai.test_openai.test_model_retrieve.json  @DataDog/ml-observability
tests/snapshots/tests.contrib.openai.test_openai.test_response.json     @DataDog/ml-observability
tests/snapshots/tests.contrib.openai.test_openai.test_response_error.json  @DataDog/ml-observability
tests/snapshots/tests.contrib.openai.test_openai.test_response_stream.json  @DataDog/ml-observability
tests/snapshots/tests.contrib.openai.test_openai.test_response_tools.json  @DataDog/ml-observability
tests/snapshots/tests.contrib.openai.test_openai.test_response_tools_stream.json  @DataDog/ml-observability
tests/snapshots/tests.contrib.openai.test_openai.test_span_finish_on_stream_error.json  @DataDog/ml-observability
tests/snapshots/tests.contrib.openai.test_openai_v1.test_completion_stream_est_tokens.json  @DataDog/ml-observability
tests/snapshots/tests.contrib.openai.test_openai_v1.test_empty_streamed_chat_completion_resp_returns.json  @DataDog/ml-observability
tests/snapshots/tests.contrib.openai.test_openai_v1.test_empty_streamed_completion_resp_returns.json  @DataDog/ml-observability
tests/snapshots/tests.contrib.openai.test_openai_v1.test_empty_streamed_response_resp_returns.json  @DataDog/ml-observability
tests/snapshots/tests.contrib.openai.test_openai_v1.test_integration_async.json  @DataDog/ml-observability
tests/snapshots/tests.contrib.openai.test_openai_v1.test_integration_service_name[None-None].json  @DataDog/ml-observability
tests/snapshots/tests.contrib.openai.test_openai_v1.test_integration_service_name[None-v0].json  @DataDog/ml-observability
tests/snapshots/tests.contrib.openai.test_openai_v1.test_integration_service_name[None-v1].json  @DataDog/ml-observability
tests/snapshots/tests.contrib.openai.test_openai_v1.test_integration_service_name[mysvc-None].json  @DataDog/ml-observability
tests/snapshots/tests.contrib.openai.test_openai_v1.test_integration_service_name[mysvc-v0].json  @DataDog/ml-observability
tests/snapshots/tests.contrib.openai.test_openai_v1.test_integration_service_name[mysvc-v1].json  @DataDog/ml-observability
tests/snapshots/tests.contrib.openai.test_openai_v1.test_integration_sync.json  @DataDog/ml-observability
tests/snapshots/tests.contrib.openai_agents.test_openai_agents.test_openai_agents.json  @DataDog/ml-observability
tests/snapshots/tests.contrib.openai_agents.test_openai_agents.test_openai_agents_streaming.json  @DataDog/ml-observability
tests/snapshots/tests.contrib.openai_agents.test_openai_agents.test_openai_agents_sync.json  @DataDog/ml-observability
tests/snapshots/tests.contrib.openai_agents.test_openai_agents.test_openai_agents_with_tool_error.json  @DataDog/ml-observability

Copy link
Contributor

github-actions bot commented Jun 26, 2025

Bootstrap import analysis

Comparison of import times between this PR and base.

Summary

The average import time from this PR is: 277 ± 3 ms.

The average import time from base is: 279 ± 3 ms.

The import time difference between this PR and base is: -1.8 ± 0.1 ms.

Import time breakdown

The following import paths have shrunk:

ddtrace.auto 1.971 ms (0.71%)
ddtrace.bootstrap.sitecustomize 1.297 ms (0.47%)
ddtrace.bootstrap.preload 1.297 ms (0.47%)
ddtrace.internal.remoteconfig.client 0.647 ms (0.23%)
ddtrace 0.673 ms (0.24%)
ddtrace.internal._unpatched 0.030 ms (0.01%)
json 0.030 ms (0.01%)
json.decoder 0.030 ms (0.01%)
re 0.030 ms (0.01%)
enum 0.030 ms (0.01%)
types 0.030 ms (0.01%)

@pr-commenter
Copy link

pr-commenter bot commented Jun 26, 2025

Benchmarks

Benchmark execution time: 2025-06-26 21:47:55

Comparing candidate commit 06e2b01 in PR branch jsimpher/dac-strip-io-from-openai with baseline commit 82ca0cf in branch main.

Found 0 performance improvements and 0 performance regressions! Performance is the same for 561 metrics, 3 unstable metrics.

@@ -164,7 +178,7 @@ def _llmobs_set_meta_tags_from_embedding(span: Span, kwargs: Dict[str, Any], res
span._set_ctx_item(OUTPUT_VALUE, "[{} embedding(s) returned]".format(len(resp.data)))

@staticmethod
def _extract_llmobs_metrics_tags(span: Span, resp: Any, span_kind: str) -> Dict[str, Any]:
def _extract_llmobs_metrics_tags(span: Span, resp: Any, span_kind: str) -> Optional[Dict[str, Any]]:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟠 Code Quality Violation

do not use Any, use a concrete type (...read more)

Use the Any type very carefully. Most of the time, the Any type is used because we do not know exactly what type is being used. If you want to specify that a value can be of any type, use object instead of Any.

Learn More

View in Datadog  Leave us feedback  Documentation

@jsimpher jsimpher marked this pull request as ready for review June 27, 2025 16:42
@jsimpher jsimpher requested review from a team as code owners June 27, 2025 16:42
@jsimpher jsimpher requested review from P403n1x87 and quinna-h June 27, 2025 16:42
Copy link
Contributor

@ncybul ncybul left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good, left some comments / questions! Lmk when you need another review!

Comment on lines 166 to 168
"engine",
"suffix",
"max_tokens",
"temperature",
"top_p",
"n",
"stream",
"logprobs",
"echo",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mind describing why we are leaving these parameters only? I guess echo seems to be related to audio models only which seems fine to leave but what about engine and suffix? I am a bit confused as to what engine is referring to as I do not see it on the list of request arguments in the Open AI API docs.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The base _EndpointHook class has its own _record_request method which does some request specific tagging on the APM span. Are we ok leaving all of those tags on the APM span? For other providers, I do not think we have any of this information (besides the model and provider), so it would be more consistent to remove this tagging; however, is the idea to keep it because we do not have this information on the LLMObs span?

I also noticed that it seems like we tag the provider as "openai.request.client" here which seems inconsistent with other integrations where we refer to this as the provider.

Comment on lines 175 to 176
def _record_request(self, pin, integration, instance, span, args, kwargs):
super()._record_request(pin, integration, instance, span, args, kwargs)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need these lines? If we remove this, won't the base class's _record_request method be called automatically (possibly other examples below).

@@ -294,7 +233,7 @@ class _ChatCompletionWithRawResponseHook(_ChatCompletionHook):

class _EmbeddingHook(_EndpointHook):
_request_arg_params = ("api_key", "api_base", "api_type", "request_id", "api_version", "organization")
_request_kwarg_params = ("model", "engine", "user")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we remove user here?

span.set_tag_str("openai.response.choices.%d.finish_reason" % choice.index, str(choice.finish_reason))
if integration.is_pc_sampled_span(span):
span.set_tag_str("openai.response.choices.%d.text" % choice.index, integration.trunc(choice.text))
integration.record_usage(span, resp.usage)
return resp
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it just me or is the logic here extremely convoluted 🤣 There are two separate conditional checks for if not resp. I know this isn't in scope of this PR but I wonder if we can refactor this a bit to make this more readable while we're already working on this part of the code! Lmk if you think it makes sense to do this in a different PR though.

@@ -355,11 +333,15 @@ def _set_token_metrics_from_streamed_response(span, response, prompts, messages,
estimated, prompt_tokens = _compute_prompt_tokens(model_name, prompts, messages)
estimated, completion_tokens = _compute_completion_tokens(response, model_name)
total_tokens = prompt_tokens + completion_tokens
span.set_metric("openai.response.usage.prompt_tokens", prompt_tokens)
span.set_metric("openai.request.prompt_tokens_estimated", int(estimated))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The estimated variable is no longer being used, was this intentional? is there any downstream impact of this? Might we worth checking with @Yun-Kim who might know more about what this is used for if anything!

@@ -133,7 +147,7 @@ def _llmobs_set_tags(
elif operation == "response":
openai_set_meta_tags_from_response(span, kwargs, response)
update_proxy_workflow_input_output_value(span, span_kind)
metrics = self._extract_llmobs_metrics_tags(span, response, span_kind)
metrics = self._extract_llmobs_metrics_tags(span, response, span_kind) or span._get_ctx_item(METRICS)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you confirm my understanding -- if the response is streamed, we expect the metrics to be on the span context and if the response is not streamed, then we need to extract the token usage from the response itself?

@pytest.mark.skipif(
parse_version(openai_module.version.VERSION) < (1, 26), reason="Stream options only available openai >= 1.26"
)
def test_chat_completion_stream_explicit_no_tokens(openai, openai_vcr, mock_tracer):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we removing this test because we no longer include token metrics on the APM span itself? I am curious, do we test in the llmobs tests that we do not include the token metrics on the LLMObs span in this case?

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

Successfully merging this pull request may close these issues.

2 participants