Skip to content

Adding support for ChatAnthropic to the langchain instrumentation#4496

Open
salaboy wants to merge 2 commits intoopen-telemetry:mainfrom
salaboy:chat-anthropic-support-langchain
Open

Adding support for ChatAnthropic to the langchain instrumentation#4496
salaboy wants to merge 2 commits intoopen-telemetry:mainfrom
salaboy:chat-anthropic-support-langchain

Conversation

@salaboy
Copy link
Copy Markdown

@salaboy salaboy commented Apr 28, 2026

Description

Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. List any dependencies that are required for this change.

Fixes #4495

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

  • test_chat_anthropic_claude_sonnet_llm_call shows testing the ChatAnthropic endpoint and the spans creations

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

@salaboy salaboy requested a review from a team as a code owner April 28, 2026 08:34
@salaboy
Copy link
Copy Markdown
Author

salaboy commented Apr 28, 2026

This would be my first contribution here, so I would appreciate any guidance from experienced maintainers :) I am happy to change and update stuff as needed. Also, I am a real person not a bot, just in case :)

Comment on lines +61 to +64
"ChatOpenAI",
"ChatBedrock",
"ChatAnthropic",
):
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Why not use constants to define the chat's names?

Copy link
Copy Markdown

@jjmmolina jjmmolina left a comment

Choose a reason for hiding this comment

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

Only a little comment

@lzchen lzchen added the gen-ai Related to generative AI label Apr 30, 2026
@lmolkova lmolkova requested a review from Copilot May 1, 2026 01:38
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds LangChain instrumentation coverage for Anthropic’s ChatAnthropic, extending the existing GenAI LangChain callback handler and accompanying VCR-based tests.

Changes:

  • Extend OpenTelemetryLangChainCallbackHandler to recognize ChatAnthropic and extract Anthropic-specific invocation parameters (e.g., model, max_tokens, stop_sequences).
  • Add a new VCR-backed test + cassette covering a ChatAnthropic invoke call and validating span attributes.
  • Update package docstring to include ChatAnthropic as a supported chat model.

Reviewed changes

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

Show a summary per file
File Description
instrumentation-genai/opentelemetry-instrumentation-langchain/src/opentelemetry/instrumentation/langchain/callback_handler.py Adds ChatAnthropic recognition and parameter extraction for proper invocation capture.
instrumentation-genai/opentelemetry-instrumentation-langchain/src/opentelemetry/instrumentation/langchain/init.py Updates module docstring to reflect Anthropic support.
instrumentation-genai/opentelemetry-instrumentation-langchain/tests/conftest.py Adds ChatAnthropic fixture and test env/VCR header filtering for Anthropic API key usage.
instrumentation-genai/opentelemetry-instrumentation-langchain/tests/test_llm_call.py Adds Anthropic chat test and span-attribute assertion helper.
instrumentation-genai/opentelemetry-instrumentation-langchain/tests/cassettes/test_chat_anthropic_claude_sonnet_llm_call.yaml Recorded Anthropic /v1/messages interaction for deterministic testing.

Comment on lines +380 to +382
assert span.attributes["gen_ai.provider.name"] == "anthropic"
assert span.attributes[gen_ai_attributes.GEN_AI_REQUEST_MAX_TOKENS] == 100
assert span.attributes[gen_ai_attributes.GEN_AI_REQUEST_TEMPERATURE] == 0.1
Copy link

Copilot AI May 1, 2026

Choose a reason for hiding this comment

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

The provider attribute is accessed via the literal key "gen_ai.provider.name". Prefer using the generated semantic convention constant (gen_ai_attributes.GEN_AI_PROVIDER_NAME) to avoid typos and keep these assertions aligned with the semconv module API (this file already uses constants for the same attribute in metric assertions).

Copilot uses AI. Check for mistakes.
Comment on lines +181 to +198
# span_exporter, start_instrumentation, chat_anthropic_claude_sonnet are coming from fixtures defined in conftest.py
@pytest.mark.vcr()
def test_chat_anthropic_claude_sonnet_llm_call(
span_exporter, start_instrumentation, chat_anthropic_claude_sonnet
):
messages = [
SystemMessage(content="You are a helpful assistant!"),
HumanMessage(content="What is the capital of France?"),
]

result = chat_anthropic_claude_sonnet.invoke(messages)

assert result.content.find("The capital of France is Paris") != -1

# verify spans
spans = span_exporter.get_finished_spans()
assert_anthropic_completion_attributes(spans[0], result)

Copy link

Copilot AI May 1, 2026

Choose a reason for hiding this comment

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

For new ChatAnthropic support, there’s only a non-streaming “happy path” VCR test. Per the GenAI instrumentation test guidelines, provider additions should also include recorded tests for streaming and for error scenarios (at least: provider error/endpoint unavailable, and a stream interrupted/closed early). Adding those tests here will prevent regressions in callback handling across ChatAnthropic variants.

Copilot generated this review using guidance from repository custom instructions.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gen-ai Related to generative AI

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

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

5 participants