-
Notifications
You must be signed in to change notification settings - Fork 931
OpenAI v2: completion hook support and minor fixes #4315
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
lmolkova
wants to merge
6
commits into
open-telemetry:main
Choose a base branch
from
lmolkova:openai-v2-hook-and-fixes
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 all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f408fcb
Add completion hook support to openai and fix v2 compat issues
lmolkova 6d21ce7
fixes
lmolkova 6c70b3d
lint
lmolkova d233923
allow new content env var vals on legacy path
lmolkova a6a316a
Merge branch 'main' into openai-v2-hook-and-fixes
lmolkova 852a965
Merge branch 'main' into openai-v2-hook-and-fixes
lmolkova 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
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
129 changes: 129 additions & 0 deletions
129
instrumentation-genai/opentelemetry-instrumentation-openai-v2/tests/test_completion_hook.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,129 @@ | ||
| # 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. | ||
|
|
||
| import os | ||
| from unittest.mock import MagicMock, patch | ||
|
|
||
| from opentelemetry.instrumentation._semconv import ( | ||
| OTEL_SEMCONV_STABILITY_OPT_IN, | ||
| _OpenTelemetrySemanticConventionStability, | ||
| ) | ||
| from opentelemetry.instrumentation.openai_v2 import OpenAIInstrumentor | ||
| from opentelemetry.util.genai.environment_variables import ( | ||
| OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT, | ||
| ) | ||
|
|
||
| from .test_utils import DEFAULT_MODEL, USER_ONLY_PROMPT | ||
|
|
||
|
|
||
| @patch.dict( | ||
| os.environ, | ||
| {OTEL_SEMCONV_STABILITY_OPT_IN: "gen_ai_latest_experimental"}, | ||
| ) | ||
| def test_custom_hook_is_called( | ||
| span_exporter, | ||
| log_exporter, | ||
| tracer_provider, | ||
| logger_provider, | ||
| meter_provider, | ||
| openai_client, | ||
| vcr, | ||
| ): | ||
| """A hook passed to instrument() is called after each chat completion.""" | ||
| hook = MagicMock() | ||
| instrumentor = OpenAIInstrumentor() | ||
| _OpenTelemetrySemanticConventionStability._initialized = False | ||
| instrumentor.instrument( | ||
| tracer_provider=tracer_provider, | ||
| logger_provider=logger_provider, | ||
| meter_provider=meter_provider, | ||
| completion_hook=hook, | ||
| ) | ||
|
|
||
| try: | ||
| with vcr.use_cassette("test_chat_completion_with_content.yaml"): | ||
| openai_client.chat.completions.create( | ||
| messages=USER_ONLY_PROMPT, | ||
| model=DEFAULT_MODEL, | ||
| stream=False, | ||
| ) | ||
| finally: | ||
| instrumentor.uninstrument() | ||
|
|
||
| hook.on_completion.assert_called_once() | ||
| kwargs = hook.on_completion.call_args.kwargs | ||
| assert kwargs["inputs"] | ||
| assert kwargs["outputs"] | ||
| assert kwargs["span"] is not None | ||
|
|
||
| # Content goes to the hook only — not to span attributes or log records | ||
| spans = span_exporter.get_finished_spans() | ||
| assert len(spans) == 1 | ||
| span_attrs = spans[0].attributes or {} | ||
| assert "gen_ai.input.messages" not in span_attrs | ||
| assert "gen_ai.output.messages" not in span_attrs | ||
|
|
||
| assert log_exporter.get_finished_logs() == () | ||
|
|
||
|
|
||
| @patch.dict( | ||
| os.environ, | ||
| { | ||
| OTEL_SEMCONV_STABILITY_OPT_IN: "gen_ai_latest_experimental", | ||
| OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT: "span_only", | ||
| }, | ||
| ) | ||
| def test_default_hook_loaded_from_env( | ||
| span_exporter, | ||
| tracer_provider, | ||
| logger_provider, | ||
| meter_provider, | ||
| openai_client, | ||
| vcr, | ||
| ): | ||
| """When no hook kwarg is given, load_completion_hook() provides the default.""" | ||
| default_hook = MagicMock() | ||
| instrumentor = OpenAIInstrumentor() | ||
| with patch( | ||
| "opentelemetry.instrumentation.openai_v2.load_completion_hook", | ||
| return_value=default_hook, | ||
| ): | ||
| instrumentor.instrument( | ||
| tracer_provider=tracer_provider, | ||
| logger_provider=logger_provider, | ||
| meter_provider=meter_provider, | ||
| # no completion_hook kwarg — should fall back to load_completion_hook() | ||
| ) | ||
|
|
||
| try: | ||
| with vcr.use_cassette("test_chat_completion_with_content.yaml"): | ||
| openai_client.chat.completions.create( | ||
| messages=USER_ONLY_PROMPT, | ||
| model=DEFAULT_MODEL, | ||
| stream=False, | ||
| ) | ||
| finally: | ||
| instrumentor.uninstrument() | ||
|
|
||
| default_hook.on_completion.assert_called_once() | ||
| kwargs = default_hook.on_completion.call_args.kwargs | ||
| assert kwargs["inputs"] | ||
| assert kwargs["outputs"] | ||
| assert kwargs["span"] is not None | ||
|
|
||
| spans = span_exporter.get_finished_spans() | ||
| assert len(spans) == 1 | ||
| span_attrs = spans[0].attributes or {} | ||
| assert "gen_ai.input.messages" in span_attrs | ||
| assert "gen_ai.output.messages" in span_attrs |
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
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.
_openai_response_format_to_output_type()hardcodes the semconv output type as the string literal "json". Forgen_ai.output.typethe semantic conventions define a well-known value set; per instrumentation-genai guideline 1000002 §4, prefer usingGenAIAttributes.GenAiOutputTypeValues.JSON.value(and other enum values) instead of raw strings.