-
Notifications
You must be signed in to change notification settings - Fork 155
feat(portkey): Support instrumentation of portkey_ai.AsyncPortkey
#2407
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
shotarok
wants to merge
3
commits into
Arize-ai:main
Choose a base branch
from
shotarok:support-portkey-async-funcs
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
3 commits
Select commit
Hold shift + click to select a range
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
66 changes: 66 additions & 0 deletions
66
...ion/openinference-instrumentation-portkey/tests/cassettes/test_async_chat_completion.yaml
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,66 @@ | ||
| interactions: | ||
| - request: | ||
| body: '{"messages":[{"role":"user","content":"What''s the weather like?"}],"model":"gpt-4o-mini"}' | ||
| headers: | ||
| accept: | ||
| - application/json | ||
| accept-encoding: | ||
| - gzip, deflate | ||
| authorization: | ||
| - Bearer OPENAI_API_KEY | ||
| connection: | ||
| - keep-alive | ||
| content-length: | ||
| - '89' | ||
| content-type: | ||
| - application/json | ||
| host: | ||
| - api.portkey.ai | ||
| user-agent: | ||
| - AsyncOpenAI/Python 2.2.0 | ||
| x-stainless-arch: | ||
| - arm64 | ||
| x-stainless-async: | ||
| - async:asyncio | ||
| x-stainless-lang: | ||
| - python | ||
| x-stainless-os: | ||
| - MacOS | ||
| x-stainless-package-version: | ||
| - 2.2.0 | ||
| x-stainless-raw-response: | ||
| - 'true' | ||
| x-stainless-read-timeout: | ||
| - '600' | ||
| x-stainless-retry-count: | ||
| - '0' | ||
| x-stainless-runtime: | ||
| - CPython | ||
| x-stainless-runtime-version: | ||
| - 3.10.0 | ||
| method: POST | ||
| uri: https://api.portkey.ai/v1/chat/completions | ||
| response: | ||
| body: | ||
| string: '{"id":"chatcmpl-AsyncTest123456789","object":"chat.completion","created":1743882894,"model":"gpt-4o-mini-2024-07-18","choices":[{"index":0,"message":{"role":"assistant","content":"I don''t have real-time data access to provide current weather updates. However, you can check a weather website or app for the latest information in your area. If you tell me your location, I can suggest typical weather patterns for this time of year!","refusal":null,"annotations":[]},"logprobs":null,"finish_reason":"stop"}],"usage":{"prompt_tokens":12,"completion_tokens":51,"total_tokens":63,"prompt_tokens_details":{"cached_tokens":0,"audio_tokens":0},"completion_tokens_details":{"reasoning_tokens":0,"audio_tokens":0,"accepted_prediction_tokens":0,"rejected_prediction_tokens":0}},"service_tier":"default","system_fingerprint":"fp_b376dfbbd5"}' | ||
| headers: | ||
| CF-RAY: | ||
| - 99984826acb4cb94-LAX | ||
| Connection: | ||
| - keep-alive | ||
| Content-Length: | ||
| - '694' | ||
| Content-Type: | ||
| - application/json;charset=UTF-8 | ||
| Date: | ||
| - Wed, 05 Nov 2025 00:37:18 GMT | ||
| Server: | ||
| - cloudflare | ||
| Vary: | ||
| - accept-encoding | ||
| alt-svc: | ||
| - h3=":443"; ma=86400 | ||
| status: | ||
| code: 200 | ||
| message: OK | ||
| version: 1 |
74 changes: 74 additions & 0 deletions
74
...ion/openinference-instrumentation-portkey/tests/cassettes/test_async_prompt_template.yaml
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,74 @@ | ||
| interactions: | ||
| - request: | ||
| body: '{"variables":{"location":"New York City"},"stream":false}' | ||
| headers: | ||
| accept: | ||
| - application/json | ||
| accept-encoding: | ||
| - gzip, deflate | ||
| connection: | ||
| - keep-alive | ||
| content-length: | ||
| - '57' | ||
| content-type: | ||
| - application/json | ||
| host: | ||
| - api.portkey.ai | ||
| user-agent: | ||
| - python-httpx/0.28.1 | ||
| method: POST | ||
| uri: https://api.portkey.ai/v1/prompts/pp-weather-pr-b74c4f/completions | ||
| response: | ||
| body: | ||
| string: '{"id":"chatcmpl-AsyncPromptTest789","object":"chat.completion","created":1743882894,"model":"gpt-4.1-2025-04-14","choices":[{"finish_reason":"stop","index":0,"message":{"content":"Based on the current weather data, New York City is experiencing partly cloudy skies with a temperature of around 72°F (22°C). There''s a light breeze from the northwest at about 8 mph. Humidity levels are moderate at around 55%. It''s a pleasant spring day overall with no precipitation expected in the immediate forecast.","role":"assistant","refusal":null,"annotations":[]},"logprobs":null}],"usage":{"completion_tokens":66,"prompt_tokens":35,"total_tokens":101,"prompt_tokens_details":{"cached_tokens":0},"completion_tokens_details":{"reasoning_tokens":0}},"service_tier":"default","system_fingerprint":"fp_test123"}' | ||
| headers: | ||
| CF-Cache-Status: | ||
| - DYNAMIC | ||
| CF-Ray: | ||
| - 943debc01e8b1492-EWR | ||
| Connection: | ||
| - keep-alive | ||
| Content-Type: | ||
| - application/json | ||
| Date: | ||
| - Thu, 22 May 2025 17:09:26 GMT | ||
| Server: | ||
| - cloudflare | ||
| Strict-Transport-Security: | ||
| - max-age=31536000; includeSubDomains; preload | ||
| Transfer-Encoding: | ||
| - chunked | ||
| Vary: | ||
| - Accept-Encoding | ||
| X-Content-Type-Options: | ||
| - nosniff | ||
| access-control-expose-headers: | ||
| - X-Request-ID | ||
| alt-svc: | ||
| - h3=":443"; ma=86400 | ||
| openai-organization: | ||
| - arize-ai-ewa7w1 | ||
| openai-processing-ms: | ||
| - '3213' | ||
| openai-version: | ||
| - '2020-10-01' | ||
| x-envoy-upstream-service-time: | ||
| - '3221' | ||
| x-ratelimit-limit-requests: | ||
| - '10000' | ||
| x-ratelimit-limit-tokens: | ||
| - '30000000' | ||
| x-ratelimit-remaining-requests: | ||
| - '9999' | ||
| x-ratelimit-remaining-tokens: | ||
| - '29999965' | ||
| x-ratelimit-reset-requests: | ||
| - 6ms | ||
| x-ratelimit-reset-tokens: | ||
| - 0s | ||
| x-request-id: | ||
| - req_24c239d275655c5f601e70346c6cd0f6 | ||
| status: | ||
| code: 200 | ||
| message: OK | ||
| version: 1 |
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
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.
Bug: Incomplete uninstrumentation leaves generation wrap active
The
_uninstrumentmethod does not properly restoreAsyncCompletions.createfrom thegenerationmodule. During instrumentation (lines 64-68),AsyncCompletions.createis wrapped in bothportkey_ai.api_resources.apis.chat_completeandportkey_ai.api_resources.apis.generationmodules. However, the uninstrument method only imports and restores from thechat_completemodule (lines 71-76), leaving thegenerationmodule'sAsyncCompletions.createmethod wrapped even after uninstrumentation. This will cause the instrumentation to remain active for async completions called through the generation module even after callinguninstrument().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.
If this is true, we would need to restore the sync function, in addition to the async function.