-
Notifications
You must be signed in to change notification settings - Fork 934
feat: OpenAI responses create instrumentation #4474
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
Open
eternalcuriouslearner
wants to merge
28
commits into
open-telemetry:main
Choose a base branch
from
eternalcuriouslearner:feat/openai-responses-create-instrumentation-first-part
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 17 commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
799e80e
wip: first draft of openai responses instrumentation.
eternalcuriouslearner 13d5d03
Merge remote-tracking branch 'upstream/main' into feat/openai-respons…
eternalcuriouslearner 89c8434
wip: converted responses to use new handler factory methods.
eternalcuriouslearner b36205d
WIP: Adding test files and refined the missing parts.
eternalcuriouslearner 0b241e3
WIP: Moving cache assertions to common utils file.
eternalcuriouslearner 9dcb021
WIP: removing the async create method instrumentation.
eternalcuriouslearner 881686b
WIP: removed the unnecessary cassette checks added context around res…
eternalcuriouslearner 0c277ac
WIP: fixing the lint in files.
eternalcuriouslearner ecc3774
WIP: fixing the precommit stuff.
eternalcuriouslearner d4ff5af
WIP: added changelog.
eternalcuriouslearner c5abec7
Merge branch 'main' into feat/openai-responses-create-instrumentation…
eternalcuriouslearner 0293f21
wip: fixing the wrap configuration.
eternalcuriouslearner 9bb3221
Merge branch 'main' into feat/openai-responses-create-instrumentation…
eternalcuriouslearner 57a7715
wip: remove pydantic based validation and convert the request to data…
eternalcuriouslearner 8558a3e
wip: delete old pydantic job.
eternalcuriouslearner 0634075
Merge branch 'main' into feat/openai-responses-create-instrumentation…
eternalcuriouslearner da22aa9
wip: removed the unwanted LLMInvocation.
eternalcuriouslearner 2daf193
wip: cleaning up the functions.
eternalcuriouslearner d9374b3
wip: cleaning up the failing tests.
eternalcuriouslearner 0520e0b
wip: fixing precommit.
eternalcuriouslearner ce8882d
wip: cleaning the precommit.
eternalcuriouslearner e74229a
polish: cleaning up tests and lint.
eternalcuriouslearner 81969b9
polish: adding span assertions for missing tests.
eternalcuriouslearner db8a212
Merge branch 'main' into feat/openai-responses-create-instrumentation…
eternalcuriouslearner fa4eec3
Merge branch 'main' into feat/openai-responses-create-instrumentation…
lzchen 019c695
Merge branch 'main' into feat/openai-responses-create-instrumentation…
eternalcuriouslearner 20f361f
polish: adding typechecks and using attributes from genai util struct…
eternalcuriouslearner bba92d9
Merge branch 'feat/openai-responses-create-instrumentation-first-part…
eternalcuriouslearner 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
71 changes: 71 additions & 0 deletions
71
...-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,71 @@ | ||
| # 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 __future__ import annotations | ||
|
|
||
| from opentelemetry.util.genai.handler import TelemetryHandler | ||
| from opentelemetry.util.genai.types import ContentCapturingMode, Error | ||
|
|
||
| from .response_extractors import ( | ||
| apply_request_attributes, | ||
| extract_params, | ||
| get_inference_creation_kwargs, | ||
| set_invocation_response_attributes, | ||
| ) | ||
| from .response_wrappers import ResponseStreamWrapper | ||
| from .utils import is_streaming | ||
|
|
||
|
|
||
| def responses_create( | ||
| handler: TelemetryHandler, | ||
| content_capturing_mode: ContentCapturingMode, | ||
| ): | ||
| """Wrap the `create` method of the `Responses` class to trace it.""" | ||
|
|
||
| capture_content = content_capturing_mode != ContentCapturingMode.NO_CONTENT | ||
|
|
||
| def traced_method(wrapped, instance, args, kwargs): | ||
| params = extract_params(**kwargs) | ||
| invocation = handler.start_inference( | ||
| **get_inference_creation_kwargs(params, instance) | ||
| ) | ||
| apply_request_attributes(invocation, params, capture_content) | ||
|
|
||
| try: | ||
| result = wrapped(*args, **kwargs) | ||
| parsed_result = _get_response_stream_result(result) | ||
|
|
||
| if is_streaming(kwargs): | ||
| return ResponseStreamWrapper( | ||
| parsed_result, | ||
| invocation, | ||
| capture_content, | ||
| ) | ||
|
|
||
| set_invocation_response_attributes( | ||
| invocation, parsed_result, capture_content | ||
| ) | ||
| invocation.stop() | ||
| return result | ||
| except Exception as error: | ||
| invocation.fail(Error(type=type(error), message=str(error))) | ||
| raise | ||
|
|
||
| return traced_method | ||
|
|
||
|
|
||
| def _get_response_stream_result(result): | ||
| if hasattr(result, "parse"): | ||
| return result.parse() | ||
| return result |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Purpose of this change? Shouldn't we test with pydantic2 if openai dropped support for 1?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The pydantic test were added previously when I was using pydantic for validating our response models. But @herin049 suggested on removing this as it might make this instrumentation tightly coupled with pydantic coming from openai library. Hence the above change was made to remove the dependency on pydantic.