-
Notifications
You must be signed in to change notification settings - Fork 830
OpenAI Responses and Compaction APIs #4042
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
Draft
iamemilio
wants to merge
6
commits into
open-telemetry:main
Choose a base branch
from
iamemilio:responses_openai
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.
+14,910
−3,045
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
762fb1a
OpenAI Responses and Compaction APIs
iamemilio f5d746f
test: responses API gracefully handles None input
iamemilio d87745d
fix: OpenAI Types Normalized with Cast
iamemilio 456aafc
openaiv2: clarify streaming contract and test corner cases
iamemilio 8913d11
openai_v2: improve readability of responses.py
iamemilio af4f76a
openai_v2: move responses wrappers into patch.py
iamemilio 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -40,6 +40,7 @@ | |
| --- | ||
| """ | ||
|
|
||
| import importlib | ||
| from typing import Collection | ||
|
|
||
| from wrapt import wrap_function_wrapper | ||
|
|
@@ -59,6 +60,10 @@ | |
| async_embeddings_create, | ||
| chat_completions_create, | ||
| embeddings_create, | ||
| async_responses_compact, | ||
| async_responses_create, | ||
| responses_compact, | ||
| responses_create, | ||
| ) | ||
|
|
||
|
|
||
|
|
@@ -128,10 +133,56 @@ def _instrument(self, **kwargs): | |
| ), | ||
| ) | ||
|
|
||
| def _uninstrument(self, **kwargs): | ||
| import openai # pylint: disable=import-outside-toplevel # noqa: PLC0415 | ||
| # Add instrumentation for the Responses API | ||
| wrap_function_wrapper( | ||
| module="openai.resources.responses", | ||
| name="Responses.create", | ||
| wrapper=responses_create( | ||
| tracer, logger, instruments, is_content_enabled() | ||
| ), | ||
| ) | ||
|
|
||
| wrap_function_wrapper( | ||
| module="openai.resources.responses", | ||
| name="AsyncResponses.create", | ||
| wrapper=async_responses_create( | ||
| tracer, logger, instruments, is_content_enabled() | ||
| ), | ||
| ) | ||
|
|
||
| # `Responses.compact` was added later in openai-python; guard so older | ||
| # supported versions don't fail instrumentation. | ||
| try: | ||
| wrap_function_wrapper( | ||
| module="openai.resources.responses", | ||
| name="Responses.compact", | ||
| wrapper=responses_compact( | ||
| tracer, logger, instruments, is_content_enabled() | ||
| ), | ||
| ) | ||
| wrap_function_wrapper( | ||
| module="openai.resources.responses", | ||
| name="AsyncResponses.compact", | ||
| wrapper=async_responses_compact( | ||
| tracer, logger, instruments, is_content_enabled() | ||
| ), | ||
| ) | ||
| except AttributeError: | ||
| pass | ||
|
|
||
| unwrap(openai.resources.chat.completions.Completions, "create") | ||
| unwrap(openai.resources.chat.completions.AsyncCompletions, "create") | ||
| unwrap(openai.resources.embeddings.Embeddings, "create") | ||
| unwrap(openai.resources.embeddings.AsyncEmbeddings, "create") | ||
| def _uninstrument(self, **kwargs): | ||
| chat_mod = importlib.import_module("openai.resources.chat.completions") | ||
| unwrap(chat_mod.Completions, "create") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. unwrap takes also strings as the wrap functions, you shouldn't need the importlib usage |
||
| unwrap(chat_mod.AsyncCompletions, "create") | ||
|
|
||
| embeddings_mod = importlib.import_module("openai.resources.embeddings") | ||
| unwrap(embeddings_mod.Embeddings, "create") | ||
| unwrap(embeddings_mod.AsyncEmbeddings, "create") | ||
|
|
||
| responses_mod = importlib.import_module("openai.resources.responses") | ||
| unwrap(responses_mod.Responses, "create") | ||
| unwrap(responses_mod.AsyncResponses, "create") | ||
| if hasattr(responses_mod.Responses, "compact"): | ||
| unwrap(responses_mod.Responses, "compact") | ||
| if hasattr(responses_mod.AsyncResponses, "compact"): | ||
| unwrap(responses_mod.AsyncResponses, "compact") | ||
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.
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.
Why? If it is the first version having Responses the instrumentation should continue to work with older versions.