SIGN IN SIGN UP

feat(anthropic): add Messages.create sync instrumentation (#4034)

* feat(anthropic): add Messages.create sync instrumentation

* polish: updating the changelog.

* polish: fxing the checks.

* polish: fixing the linting, test failures and commit checks.

* wip: fixing generate and lint errors.

* fixing changelog.

* polish: removing unnecessary pyright ignore comments.

* polish: downgrading the sdk version to 0.16.0.

* Refactor Anthropic instrumentation to use TelemetryHandler for tracing

- Replaced direct tracer and logger initialization with TelemetryHandler in AnthropicInstrumentor.
- Updated messages_create function to utilize TelemetryHandler for LLM invocation tracking.
- Removed unused span handling functions and attributes to streamline code.
- Enhanced attribute capturing for LLM requests and responses.

* Refactor test setup and instrumentor functionality

- Removed unused trace import from conftest.py and updated tracer_provider fixture to return the provider instead of yielding it.
- Enhanced test_instrument_with_no_providers to ensure proper instrument/uninstrument cycle while passing providers for a clean test environment.

* wip: updating requirements for avoiding test failures.

* wip: fixing tox issues.

* Refactor request model retrieval in messages_create function

- Improved readability by restructuring the assignment of request_model in the messages_create function.
- Ensured consistent handling of model attributes from both the attributes dictionary and kwargs.

* Enhance LLM request attribute extraction in get_llm_request_attributes function

- Updated the return type to include AttributeValue for better type safety.
- Expanded the docstring to detail the OpenTelemetry semantic conventions for LLM requests and server attributes.
- Included descriptions for GenAI attributes and server attributes to improve clarity and documentation.

* Add integration tests for Anthropic API and update pytest configuration

- Introduced a new test file for integration tests that require a valid ANTHROPIC_API_KEY.
- Implemented a pytest hook to skip integration tests if the API key is not set or is a placeholder.
- Updated pyproject.toml to include pytest configuration for test paths and markers for integration tests.
- Enhanced conftest.py to support skipping integration tests based on API key availability.

* Refactor integration tests for Anthropic API

- Removed commented-out imports and improved assertion readability in test_integration.py.
- Streamlined attribute checks for spans to enhance clarity and maintainability.
- Ensured consistent validation of response attributes in integration tests.

* Enable pylint for integration tests in test_integration.py

- Added a pylint directive to disable the redefined-outer-name warning, improving code quality and maintainability.

* Refactor LLM request attribute handling and improve documentation

- Updated the messages_create function to ensure request_model is explicitly converted to a string for better type consistency.
- Cleaned up the docstring in get_llm_request_attributes to remove unnecessary whitespace and enhance readability.
- Improved clarity in the documentation of GenAI and server attributes included in the returned dictionary.

* Add TODO for logger_provider in TelemetryHandler

- Added a comment to the AnthropicInstrumentor class to indicate the need for a logger_provider in the TelemetryHandler to capture content events.

* Remove integration test markers and related code from the Anthropic instrumentation package. The integration tests have been deleted, and the pytest configuration has been updated to reflect these changes.

* Remove unnecessary blank lines in the VCR fixture in the Anthropic instrumentation test configuration.

* Refactor Anthropic instrumentation: remove content capture utility and related references. Updated messages_create function to eliminate unused parameter.

* Add parsing functions for input and output messages in Anthropic instrumentation

Implemented `parse_input_messages` and `parse_output_message` functions to handle the formatting of input and output messages. Updated the `messages_create` function to utilize these new parsing utilities, enhancing the structure and clarity of message handling.

* Revert "Add parsing functions for input and output messages in Anthropic instrumentation"

This reverts commit e4b202043de4a4202ce1bfa9e44621cd912ec5cf.

* Enhance Anthropic instrumentation by adding parameter extraction and type hints. Introduced `extract_params` function to robustly handle message creation parameters. Updated `messages_create` function to utilize extracted parameters and improved type annotations for better clarity. Updated tests and cassettes to reflect changes in model names and ensure compatibility with new functionality.
T
Teja committed
81db857c9c7fe388bd63b30fdd90bc0b551662da
Parent: 6d822a1
Committed by GitHub <noreply@github.com> on 1/15/2026, 12:30:19 AM