Skip to content

feat(langchain): add ChatAnthropic support to langchain instrumentation#188

Open
bhumikadangayach wants to merge 7 commits into
open-telemetry:mainfrom
bhumikadangayach:feat/chat-anthropic-langchain-support
Open

feat(langchain): add ChatAnthropic support to langchain instrumentation#188
bhumikadangayach wants to merge 7 commits into
open-telemetry:mainfrom
bhumikadangayach:feat/chat-anthropic-langchain-support

Conversation

@bhumikadangayach

Copy link
Copy Markdown
Contributor

Fixes #139

Summary

Adds ChatAnthropic to the list of supported chat models in the LangChain callback handler. Previously only ChatOpenAI and ChatBedrock were instrumented; Anthropic users received no spans at all.

Changes

  • Add ChatAnthropic to the allowlist in on_chat_model_start
  • Add "model" param key for model name extraction (Anthropic uses model, not model_name or model_id)
  • Add "stop_sequences" fallback for stop param (Anthropic's native param name)
  • Add "max_tokens" fallback for max tokens param
  • Add langchain-anthropic to test requirements
  • Add ChatAnthropic fixture and VCR cassette test

Notes

The implementation approach follows the existing pattern for ChatBedrock — provider-specific param key fallbacks in on_chat_model_start. The original fix was prototyped in opentelemetry-python-contrib#4496 by @salaboy before the repo migration; this ports and updates it to the current codebase structure.

Adds ChatAnthropic to the list of supported chat models in the
LangChain callback handler. Previously only ChatOpenAI and ChatBedrock
were instrumented; Anthropic users received no spans.

Changes:
- Add ChatAnthropic to the allowlist in on_chat_model_start
- Add 'model' param key for model name extraction (Anthropic uses 'model',
  not 'model_name' or 'model_id')
- Add 'stop_sequences' fallback for stop param (Anthropic's param name)
- Add 'max_tokens' fallback for max tokens param
- Add langchain-anthropic to test requirements
- Add ChatAnthropic fixture and VCR cassette test

Fixes open-telemetry#139

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Adds ChatAnthropic coverage to the LangChain GenAI instrumentation so Anthropic chat invocations emit spans and model/request parameters are captured consistently with existing OpenAI/Bedrock handling.

Changes:

  • Extend the on_chat_model_start allowlist and parameter key extraction to support ChatAnthropic (model/stop/max_tokens fallbacks).
  • Add a ChatAnthropic test fixture, dependency, and a VCR-backed test/cassette to validate span creation and model attribution.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
instrumentation/opentelemetry-instrumentation-genai-langchain/src/opentelemetry/instrumentation/genai/langchain/callback_handler.py Adds ChatAnthropic to supported chat models and introduces Anthropic-specific request param fallbacks.
instrumentation/opentelemetry-instrumentation-genai-langchain/tests/test_llm_call.py Adds a VCR-backed test asserting Anthropic invocation produces a span and captures the request model.
instrumentation/opentelemetry-instrumentation-genai-langchain/tests/conftest.py Adds a ChatAnthropic fixture for use in tests.
instrumentation/opentelemetry-instrumentation-genai-langchain/tests/requirements.txt Adds langchain-anthropic to the test dependencies.
instrumentation/opentelemetry-instrumentation-genai-langchain/tests/cassettes/test_chat_anthropic_claude_sonnet_llm_call.yaml Adds the recorded cassette for the new Anthropic test.

frequency_penalty = params.get("frequency_penalty")
presence_penalty = params.get("presence_penalty")
stop_sequences = params.get("stop")
stop_sequences = params.get("stop") or params.get("stop_sequences")

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Applied in 242dc03.

seed = params.get("seed")
temperature = params.get("temperature")
max_tokens = params.get("max_completion_tokens")
max_tokens = params.get("max_completion_tokens") or params.get("max_tokens")

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Applied in 242dc03.

spans = span_exporter.get_finished_spans()
assert len(spans) == 1
span = spans[0]
assert span.attributes.get("gen_ai.request.model") == "claude-sonnet-4-5"

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Applied in 242dc03.

@opentelemetry-pr-dashboard

Copy link
Copy Markdown

This PR has review comments. Review suggestions, whether from maintainers or automated reviewers, aren't always correct or required. Please evaluate each comment on its merits, then make sure each thread has a clear outcome.

For example, link to the commit if you applied a suggestion, explain why it wasn't applied, or ask a follow-up question.

Automation flags a PR for human review once every review thread has a reply or is marked as resolved.

Status across open PRs is visible on the pull request dashboard.

@eternalcuriouslearner

Copy link
Copy Markdown
Contributor

question(non-blocking): Can you please check if we need to add tool support for anthropic?

@bhumikadangayach

Copy link
Copy Markdown
Contributor Author

@eternalcuriouslearner Good question — the existing on_llm_end handler already captures tool calls via chat_generation.message.tool_calls, which reads from LangChain's unified message format and is provider-agnostic. So tool calls through ChatAnthropic should flow through that path without additional changes. That said, I haven't tested a tool-calling scenario with ChatAnthropic specifically — happy to add a test for it if you'd like that coverage before merging.

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.

Support ChatAnthropic for Langchain instrumentation (opentelemetry-instrumentation-langchain)

3 participants