Skip to content

GenAI Utils | Inference Type and Span Creation#3768

Merged
aabmass merged 43 commits intoopen-telemetry:mainfrom
zhirafovod:util-genai-inference-clean
Oct 10, 2025
Merged

GenAI Utils | Inference Type and Span Creation#3768
aabmass merged 43 commits intoopen-telemetry:mainfrom
zhirafovod:util-genai-inference-clean

Conversation

@keith-decker
Copy link
Copy Markdown
Contributor

Description

The GenAI Utils package will include boilerplate and helpers to standardize instrumentation for Generative AI.
This PR provides APIs to minimize the work needed to instrument genai libraries. It provides helpers for starting and stopping LLM invocations, it generates spans aligned with semconvs.

Metrics and Events to come on future PRs

Type of change

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

How Has This Been Tested?

  • Basic unit test for starting and stopping LLM - Spans
  • Unit test for parent/child invocations

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

@keith-decker
Copy link
Copy Markdown
Contributor Author

A cleaned up version of: #3732

  • Flattened LLMInvocation to not use dictionary keys
  • Removed the parent run_id, use OTEL context to manage parent/child relationships on spans
  • Return the run_id on creation if one is not provided, in order to specify which span to end on finish

@aabmass
Copy link
Copy Markdown
Member

aabmass commented Sep 18, 2025

@keith-decker thing I was mentioning in the SIG:
Looks like you already figured out the context manager thing but figured I'd share what I wrote:

def _with_instrumentation(
self,
instance: client.PredictionServiceClient
| client_v1beta1.PredictionServiceClient,
args: Any,
kwargs: Any,
):

It's used for both sync and async wrapper, and would work for streaming as well.

@DylanRussell
Copy link
Copy Markdown
Contributor

This looks good to me now. Just added a few comments

@keith-decker keith-decker requested a review from aabmass October 3, 2025 16:54
@keith-decker
Copy link
Copy Markdown
Contributor Author

@aabmass genai-file upload on python 3.9 seems to be flakey. I've seen it fail occasionally, and I don't have the ability to restart the test.

@aabmass
Copy link
Copy Markdown
Member

aabmass commented Oct 9, 2025

Sorry about that tracking here #3812 (no idea why)

Copy link
Copy Markdown
Member

@aabmass aabmass left a comment

Choose a reason for hiding this comment

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

Mostly nits, thanks for your patience!

@aabmass aabmass merged commit e3d3817 into open-telemetry:main Oct 10, 2025
638 checks passed
sightseeker added a commit to sightseeker/opentelemetry-python-contrib that referenced this pull request Mar 11, 2026
* cherry pick changes from previous PR

* move span utils to new file

* remove span state, use otel context for parent/child

* flatten LLMInvocation to use attributes instead of dict keys

* helper function and docstrings

* refactor: store span and context token in LLMInvocation instead of SpanGenerator

* refactor: rename prompts/chat_generations to input_messages/output_messages for clarity

* refactor: simplify TelemetryHandler API by moving invocation data management to LLMInvocation class

* refactor: update relative imports to absolute imports

* Update handler to use a context manager instead of start_llm and stop_llm

* resolve tox -e doc failure

* safeguard against empty request-model

* fix tox typecheck errors for utils

* refactor: move tracer to generator, clean up dead code

* remove unused linting hint

* back off stricter request-model requirements

* reintroduce manual start/stop for langchain callback flow

* clean up context handler, clarify unit tests

* remove generator concept

* update token types

* code cleanup

* Refactor TestTelemetryHandler to use instance method for span exporter setup

* refactor: remove unused type properties

* refactor: update TelemetryHandler initialization to remove **kwargs

* refactor: remove tracer variable

* refactor: code style updates

* refactor: replace json.dumps with gen_ai_json_dumps for message serialization

* refactor: update span lifecycle to use sdk over setting context manually

* refactor: don't reinvent span attribute assignment

* refactor: pylint update for python 3.13

* Revert "refactor: update span lifecycle to use sdk over setting context manually"

This reverts commit be8620b.

---------

Co-authored-by: Aaron Abbott <aaronabbott@google.com>
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.

6 participants