Skip to content

Add OpenAI embeddings instrumentation #3461

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 16 commits into
base: main
Choose a base branch
from

Conversation

drewby
Copy link
Member

@drewby drewby commented May 4, 2025

Description

This PR adds instrumentation for OpenAI's embeddings API in the GenAI instrumentation suite. The implementation follows the OpenTelemetry semantic conventions for generative AI systems and provides automatic instrumentation for the OpenAI Python client when using embeddings functionality.

The implementation captures important metadata about embedding operations including model, dimensions, and relevant timing information while respecting sensitive data handling practices.

  • Added instrumentation for both synchronous and asynchronous OpenAI embedding API calls
  • Implemented span and metrics using existing attributes, with two new custom:
    • ai.embedding.dimensions - Number of dimensions in the embedding vectors
    • ai.embedding.encoding_format - The encoding format of the embedding vectors response (base64 or float)
  • Capturing input text content (disabled by default for privacy)
  • Added a usage example called embeddings

Type of change

  • New feature (non-breaking change which adds functionality)

How Has This Been Tested?

  • Unit tests using mock responses to verify proper span creation and attribute population
  • Integration tests with the OpenAI client against a mock server
  • Manual testing using examples/embeddings with real OpenAI service

Does This PR Require a Core Repo Change?

  • Yes. - Link to PR:
  • No.

Checklist:

See contributing.md for styleguide, changelog guidelines, and more.

  • Followed the style guidelines of this project
  • Changelogs have been updated
  • Unit tests have been added
  • Documentation has been updated

Copy link
Contributor

@lmolkova lmolkova left a comment

Choose a reason for hiding this comment

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

thank you!!!

# Emit input event
input_event_attributes = {
GenAIAttributes.GEN_AI_SYSTEM: GenAIAttributes.GenAiSystemValues.OPENAI.value,
EventAttributes.EVENT_NAME: "gen_ai.embeddings.input",
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd postpone defining any new events until we have clarity on open-telemetry/semantic-conventions#2010

Also why not gen_ai.user.message?

Copy link
Member Author

Choose a reason for hiding this comment

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

Recording inputs/outputs is a fundamental feature we discussed, so I think we need to record as something. The current implementation for chat completions is events, so that would be the most consistent until we decide on 2010. If the decision is to move to attributes, we are going to need to do breaking change PR anyway.

I could reuse the existing event name, but its not really a user message in the same way as text completions.


output_event_attributes = {
GenAIAttributes.GEN_AI_SYSTEM: GenAIAttributes.GenAiSystemValues.OPENAI.value,
EventAttributes.EVENT_NAME: "gen_ai.embeddings.output",
Copy link
Contributor

Choose a reason for hiding this comment

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

see my prev comment, I'd rather not define any new events for now

GenAIAttributes.GEN_AI_SYSTEM: GenAIAttributes.GenAiSystemValues.OPENAI.value,
GenAIAttributes.GEN_AI_REQUEST_MODEL: span_attributes[
GenAIAttributes.GEN_AI_REQUEST_MODEL
],
}

if "gen_ai.embeddings.dimensions" in span_attributes:
Copy link
Contributor

Choose a reason for hiding this comment

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

we can't use span attributes to record metrics - spans are sampled and won't have any attributes when sampled out.

Copy link
Member Author

Choose a reason for hiding this comment

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

The parameter is called span_attributes, but it is from the previous PR.

Its just a dictionary created during the request by chat_completions_create and now embeddings_create, so not subject to sampling. I renamed it here in _record_metrics to just request_attributes for clarity. If needed, I can rename it in the create methods for better readability as well.

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

Successfully merging this pull request may close these issues.

9 participants