Skip to content

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

Open
bhumikadangayach wants to merge 13 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 13 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.

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.

@eternalcuriouslearner

Copy link
Copy Markdown
Contributor

@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.

That would be great. Please add a test with vcr cassettes if possible. I just want to ensure we are supporting the current workflows this instrumentation supports for bedrock and openai.

@bhumikadangayach

Copy link
Copy Markdown
Contributor Author

@eternalcuriouslearner Added the VCR tool-calling test for ChatAnthropic. While validating it, found a real bug: the finish_reason extraction was looking for Bedrock's camelCase stopReason key, but ChatAnthropic's response_metadata uses snake_case stop_reason — so tool-call responses were falling through to unknown instead of the correct reason. Fixed the key lookup and confirmed the fix with the new test. All checks are green now.

@rads-1996

Copy link
Copy Markdown
Contributor

@bhumikadangayach Some of these changes have been addressed in PR - #129. Do you mind rebasing, I believe the tests still do apply and probably the changes for stop sequences. Thanks.

@eternalcuriouslearner

Copy link
Copy Markdown
Contributor

@bhumikadangayach Some of these changes have been addressed in PR - #129. Do you mind rebasing, I believe the tests still do apply and probably the changes for stop sequences. Thanks.

@rads-1996, @wrisa can you please take a look into this pr when you have sometime. Thanks a lot in advance.

@bhumikadangayach bhumikadangayach force-pushed the feat/chat-anthropic-langchain-support branch from 7544771 to bc6b94c Compare July 9, 2026 09:50
@bhumikadangayach

Copy link
Copy Markdown
Contributor Author

Rebased onto main — thanks for the heads up on #129. The allowlist change was fully superseded by the generic model key lookup there, so I dropped it. Kept the stop_sequences/max_tokens None-check fixes and the Anthropic tool-calling test/fixture, since those weren't covered by #129. All local tests pass (156/156) against the rebased code.

@eternalcuriouslearner

Copy link
Copy Markdown
Contributor

howdy @bhumikadangayach I am looking into this sorry for the delay. Will be getting back on this.

@rads-1996 rads-1996 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.

LGTM. Nit: Perhaps the changelog can be rephrased, since the callback handler mostly handles None conditions.

@salaboy

salaboy commented Jul 10, 2026 via email

Copy link
Copy Markdown

@bhumikadangayach bhumikadangayach force-pushed the feat/chat-anthropic-langchain-support branch from a5a5ed3 to 03e78f5 Compare July 11, 2026 14:55
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
@bhumikadangayach bhumikadangayach force-pushed the feat/chat-anthropic-langchain-support branch from 03e78f5 to 3961919 Compare July 11, 2026 15:01
@bhumikadangayach

Copy link
Copy Markdown
Contributor Author

Thanks for the review, @rads-1996! Good point on the changelog — will tighten the wording to reflect that this is mostly correcting existing None-handling rather than adding new behavior. And @salaboy — glad this finally landed properly, appreciate you flagging the original gap back in the contrib repo!

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)

5 participants