-
Notifications
You must be signed in to change notification settings - Fork 970
Implement OpenAI Responses API instrumentation and examples #4166
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
vasantteja
wants to merge
29
commits into
open-telemetry:main
from
vasantteja:feat/instrument-openai-responses
Closed
Changes from all commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
f3fbd78
Implement OpenAI Responses API instrumentation and examples
vasantteja 2e568bb
Add support for OpenAI Responses API instrumentation in CHANGELOG.md
vasantteja 0f670d9
Enhance OpenAI Responses API instrumentation with version checks
vasantteja 8987864
Refactor OpenAI instrumentation code and add comments for clarity
vasantteja 02a80cd
Refactor OpenAI instrumentation code for improved readability
vasantteja 88c7908
Update OpenAI package version and refactor span name retrieval
vasantteja 65ee9c0
Enhance OpenAI instrumentation with improved span name handling and c…
vasantteja e4378e8
Merge branch 'main' into feat/instrument-openai-responses
vasantteja 9bbdd62
Implement tracing for OpenAI Responses.retrieve method
vasantteja 73df7d6
Add TODO for future migration of Responses instrumentation
vasantteja 3c0c4bb
Add responses instrumentation for OpenAI API
vasantteja c15627f
Refactor metrics recording and improve response handling in OpenAI in…
vasantteja 5e26cdd
Merge remote-tracking branch 'upstream/main' into feat/instrument-ope…
vasantteja 3f69b56
Enhance OpenAI instrumentation with new response handling and telemet…
vasantteja 9eeac24
Refactor OpenAI instrumentation to enhance content capture and teleme…
vasantteja e2604b6
Remove example files for OpenAI Responses API instrumentation.
vasantteja 8f4679e
Refactor OpenAI instrumentation to improve content capture and respon…
vasantteja 11d1b3c
regerating cassettes and fixing failing tests.
vasantteja bc1a604
Update OpenAI instrumentation to enhance dependency management and im…
vasantteja 67b49ed
Merge branch 'main' into feat/instrument-openai-responses
vasantteja 0d77913
Enhance OpenAI response handling by adding new extraction and attribu…
vasantteja a8c5f43
Refactor OpenAI response handling by modularizing extraction and resp…
vasantteja 4962a9f
Merge branch 'main' into feat/instrument-openai-responses
vasantteja f2f0b48
Refactor OpenAI instrumentation to remove optional dependency handlin…
vasantteja 9ac718a
Merge branch 'main' into feat/instrument-openai-responses
vasantteja 6cbdadd
Merge branch 'main' into feat/instrument-openai-responses
vasantteja 3b38509
Update opentelemetry-util-genai version to >=0.3b0, <0.4b0 and enhanc…
vasantteja fc065f8
Refactor OpenAI response handling to use OpenAI attributes and improv…
vasantteja b9c0c41
Merge branch 'main' into feat/instrument-openai-responses
vasantteja File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
...-instrumentation-openai-v2/src/opentelemetry/instrumentation/openai_v2/patch_responses.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| # Copyright The OpenTelemetry Authors | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| from typing import TYPE_CHECKING | ||
|
|
||
| from opentelemetry.semconv._incubating.attributes import ( | ||
| gen_ai_attributes as GenAIAttributes, | ||
| ) | ||
| from opentelemetry.util.genai.types import Error, LLMInvocation | ||
|
|
||
| from .response_extractors import ( | ||
| GEN_AI_USAGE_CACHE_CREATION_INPUT_TOKENS, | ||
| GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS, | ||
| OPENAI, | ||
| _extract_input_messages, | ||
| _extract_output_type, | ||
| _extract_system_instruction, | ||
| _set_invocation_response_attributes, | ||
| ) | ||
| from .response_wrappers import ResponseStreamWrapper | ||
| from .utils import get_llm_request_attributes, is_streaming | ||
|
|
||
| if TYPE_CHECKING: | ||
| from opentelemetry.util.genai.handler import TelemetryHandler | ||
|
|
||
| __all__ = [ | ||
| "responses_create", | ||
| "_set_invocation_response_attributes", | ||
| "GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS", | ||
| "GEN_AI_USAGE_CACHE_CREATION_INPUT_TOKENS", | ||
| ] | ||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Patch functions | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
|
|
||
| def responses_create( | ||
| handler: "TelemetryHandler", | ||
| capture_content: bool, | ||
|
vasantteja marked this conversation as resolved.
|
||
| ): | ||
| """Wrap the `create` method of the `Responses` class to trace it.""" | ||
| # https://github.com/openai/openai-python/blob/dc68b90655912886bd7a6c7787f96005452ebfc9/src/openai/resources/responses/responses.py#L828 | ||
|
|
||
| def traced_method(wrapped, instance, args, kwargs): | ||
| if Error is None or LLMInvocation is None: | ||
| raise ModuleNotFoundError( | ||
| "opentelemetry.util.genai.types is unavailable" | ||
| ) | ||
|
|
||
| operation_name = GenAIAttributes.GenAiOperationNameValues.CHAT.value | ||
| span_attributes = get_llm_request_attributes( | ||
| kwargs, | ||
| instance, | ||
| operation_name, | ||
| ) | ||
| output_type = _extract_output_type(kwargs) | ||
| if output_type: | ||
| span_attributes[GenAIAttributes.GEN_AI_OUTPUT_TYPE] = output_type | ||
| request_model_attribute = span_attributes.get( | ||
| GenAIAttributes.GEN_AI_REQUEST_MODEL | ||
| ) | ||
| request_model = ( | ||
| request_model_attribute | ||
| if isinstance(request_model_attribute, str) | ||
| else None | ||
| ) | ||
| streaming = is_streaming(kwargs) | ||
|
|
||
| invocation = handler.start_llm( | ||
| LLMInvocation( | ||
| request_model=request_model, | ||
| operation_name=operation_name, | ||
| provider=OPENAI, | ||
| input_messages=_extract_input_messages(kwargs) | ||
| if capture_content | ||
| else [], | ||
| system_instruction=_extract_system_instruction(kwargs) | ||
| if capture_content | ||
| else [], | ||
| attributes=span_attributes.copy(), | ||
| ) | ||
| ) | ||
|
|
||
| try: | ||
| result = wrapped(*args, **kwargs) | ||
| if hasattr(result, "parse"): | ||
| parsed_result = result.parse() | ||
| else: | ||
| parsed_result = result | ||
|
|
||
| if streaming: | ||
| return ResponseStreamWrapper( | ||
| parsed_result, | ||
| handler, | ||
| invocation, | ||
| capture_content, | ||
| ) | ||
|
|
||
| _set_invocation_response_attributes( | ||
| invocation, parsed_result, capture_content | ||
| ) | ||
| handler.stop_llm(invocation) | ||
| return result | ||
|
|
||
| except Exception as error: | ||
| handler.fail_llm( | ||
| invocation, Error(message=str(error), type=type(error)) | ||
| ) | ||
| raise | ||
|
|
||
| return traced_method | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.