Skip to content

Conversation

wrisa
Copy link
Contributor

@wrisa wrisa commented Jul 30, 2025

Description

Taking inspiration from traceloop's openllmetry support for langchain instrumentation, added callback handler support for llm start and end to generate span with attributes(no content) listed here.

Attributes added on span:
-> gen_ai.operation.name: Str(chat)
-> gen_ai.request.model: Str(gpt-3.5-turbo)
-> gen_ai.request.top_p: Double(0.9)
-> gen_ai.request.frequency_penalty: Double(0.5)
-> gen_ai.request.presence_penalty: Double(0.5)
-> gen_ai.request.stop_sequences: Slice(["\n","Human:","AI:"])
-> gen_ai.request.seed: Int(100)
-> gen_ai.request.max_tokens: Int(100)
-> gen_ai.provider.name: Str(openai)
-> gen_ai.request.temperature: Double(0.1)
-> gen_ai.response.finish_reasons: Slice(["stop"])
-> gen_ai.response.model: Str(gpt-3.5-turbo-0125)
-> gen_ai.response.id: Str(chatcmpl-Bz8yrvPnydD9pObv625n2CGBPHS13)
-> gen_ai.usage.input_tokens: Int(24)
-> gen_ai.usage.output_tokens: Int(7)

Please refer complete span attributes o/p: https://docs.google.com/document/d/13SXuf4iZSl2MqWUJtmoDEqFxuHZ1Y8hykW_ctlykcDE/edit?tab=t.0

We have just added span support in this PR for now to keep this PR concise and in future we will have metric and log support PRs.

Fixes # (issue)

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

  • From test folder /opentelemetry-python-contrib/instrumentation-genai/opentelemetry-instrumentation-langchain/tests run pytest -rP test_llm_call.py

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

@wrisa wrisa marked this pull request as ready for review August 1, 2025 01:50
@wrisa wrisa requested a review from a team as a code owner August 1, 2025 01:50
Copy link
Member

@pmcollins pmcollins left a comment

Choose a reason for hiding this comment

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

Thanks for putting this together. Added some comments.

@wrisa wrisa force-pushed the genai-instrumentation-langchain-spans branch from 3940934 to 072b0fd Compare August 4, 2025 21:43
@wrisa wrisa force-pushed the genai-instrumentation-langchain-spans branch 3 times, most recently from fed5254 to 01caea4 Compare August 18, 2025 15:26
@wrisa wrisa force-pushed the genai-instrumentation-langchain-spans branch 2 times, most recently from 3e1255c to d653fed Compare September 4, 2025 02:52
Copy link
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.

My biggest concern is if this is leaking memory otherwise just some small requests

@wrisa wrisa force-pushed the genai-instrumentation-langchain-spans branch from cce6652 to 2be6a8b Compare September 4, 2025 23:55
@wrisa
Copy link
Contributor Author

wrisa commented Sep 5, 2025

Action items from Aug 28 2025 Python SIG:

  • Explain value of LLM Invocation in LangChain vs client-instrumentation (i.e. openai)
    Added aws bedrock support as its specific instrumentation is not yet available
  • Unit-test showing both openai/bedrock providers to match telemetry
    Added aws bedrock support
  • How TraceLoop/OpenInference do it, document their approach
    It has both langchain and openai instrumentation available but it produces only langchain span. Uses SUPPRESS_LANGUAGE_MODEL_INSTRUMENTATION_KEY in context_api.
  • Opt-in telemetry for a short term and long-term strategy to avoid telemetry duplication.
    Add similar key SUPPRESS_LANGUAGE_MODEL_INSTRUMENTATION_KEY = "suppress_language_model_instrumentation" in semantic conventions. Implement logic to set value true in langchain instrumentation and later add support to check in downstream instrumentation(eg:openai-v2) if value already true then skip telemetry
  • we’ll provide both testing with another llm and opt-in for filing attributes.
    WIP

@wrisa wrisa force-pushed the genai-instrumentation-langchain-spans branch from 520a437 to 3508a66 Compare September 9, 2025 15:04
@wrisa
Copy link
Contributor Author

wrisa commented Sep 9, 2025

Sept 9 2025 SEMCOV meeting discussion.

  • we’ll provide both testing with another llm and opt-in for filing attributes.
    Currently ChatOpenAI and ChatBedrock are supported and telemetry for other providers/LLM are skipped and they will be supported in future. Test added for unsupported(gemini)

@wrisa wrisa force-pushed the genai-instrumentation-langchain-spans branch from a44e6b7 to bd2cc41 Compare September 9, 2025 20:11
@wrisa wrisa force-pushed the genai-instrumentation-langchain-spans branch from d68026e to d5771dd Compare September 19, 2025 19:44
@aabmass aabmass enabled auto-merge (squash) September 19, 2025 21:16
@aabmass aabmass merged commit 60a670f into open-telemetry:main Sep 19, 2025
631 of 632 checks passed
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.