-
Notifications
You must be signed in to change notification settings - Fork 3
fix(client): harden artifacts.get and follow Modal 303 long requests #201
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
dineshreddy91
wants to merge
1
commit into
main
Choose a base branch
from
fix/orion-artifacts-303-polling
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
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 | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -26,33 +26,124 @@ | |||||||||||||||||||||||||||||||||
| # via `extra_body`. | ||||||||||||||||||||||||||||||||||
| _VLM_EXTRA_KEYS: frozenset[str] = frozenset({"skills", "toolsets", "models"}) | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| def _patch_create(create_fn: Any) -> Any: | ||||||||||||||||||||||||||||||||||
| """Wrap an OpenAI ``create`` callable to accept VLM Run-specific kwargs. | ||||||||||||||||||||||||||||||||||
| # Default wall-clock budget for following a Modal 303 long-request poll URL. | ||||||||||||||||||||||||||||||||||
| _DEFAULT_LONG_REQUEST_TIMEOUT = 900.0 | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| def _pop_vlm_extra_kwargs(kwargs: dict[str, Any]) -> None: | ||||||||||||||||||||||||||||||||||
| """Move VLM-specific kwargs into ``extra_body`` in-place.""" | ||||||||||||||||||||||||||||||||||
| vlm_kwargs = {k: kwargs.pop(k) for k in _VLM_EXTRA_KEYS if k in kwargs} | ||||||||||||||||||||||||||||||||||
| if vlm_kwargs: | ||||||||||||||||||||||||||||||||||
| kwargs["extra_body"] = {**(kwargs.get("extra_body") or {}), **vlm_kwargs} | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| def _status_code_from_exc(exc: BaseException) -> int | None: | ||||||||||||||||||||||||||||||||||
| """Best-effort extract of an HTTP status code from an OpenAI/httpx error.""" | ||||||||||||||||||||||||||||||||||
| status = getattr(exc, "status_code", None) | ||||||||||||||||||||||||||||||||||
| if isinstance(status, int): | ||||||||||||||||||||||||||||||||||
| return status | ||||||||||||||||||||||||||||||||||
| response = getattr(exc, "response", None) | ||||||||||||||||||||||||||||||||||
| if response is not None: | ||||||||||||||||||||||||||||||||||
| status = getattr(response, "status_code", None) | ||||||||||||||||||||||||||||||||||
| if isinstance(status, int): | ||||||||||||||||||||||||||||||||||
| return status | ||||||||||||||||||||||||||||||||||
| return None | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| def _headers_from_exc(exc: BaseException) -> dict[str, Any]: | ||||||||||||||||||||||||||||||||||
| """Best-effort extract of response headers from an OpenAI/httpx error.""" | ||||||||||||||||||||||||||||||||||
| response = getattr(exc, "response", None) | ||||||||||||||||||||||||||||||||||
| if response is None: | ||||||||||||||||||||||||||||||||||
| return {} | ||||||||||||||||||||||||||||||||||
| headers = getattr(response, "headers", None) or {} | ||||||||||||||||||||||||||||||||||
| try: | ||||||||||||||||||||||||||||||||||
| return dict(headers) | ||||||||||||||||||||||||||||||||||
| except Exception: | ||||||||||||||||||||||||||||||||||
| return {} | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| def _parse_chat_completion(body: bytes) -> Any: | ||||||||||||||||||||||||||||||||||
| """Parse poll-response bytes into an OpenAI ChatCompletion object.""" | ||||||||||||||||||||||||||||||||||
| import json | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| from openai.types.chat import ChatCompletion | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| payload = json.loads(body) | ||||||||||||||||||||||||||||||||||
| return ChatCompletion.model_validate(payload) | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| def _follow_long_request_303( | ||||||||||||||||||||||||||||||||||
| exc: BaseException, | ||||||||||||||||||||||||||||||||||
| *, | ||||||||||||||||||||||||||||||||||
| api_key: str | None, | ||||||||||||||||||||||||||||||||||
| timeout: float, | ||||||||||||||||||||||||||||||||||
| ) -> Any: | ||||||||||||||||||||||||||||||||||
| """Poll the Modal ``Location`` URL from a 303 and return the ChatCompletion.""" | ||||||||||||||||||||||||||||||||||
| from vlmrun.client.long_request import ( | ||||||||||||||||||||||||||||||||||
| extract_location, | ||||||||||||||||||||||||||||||||||
| poll_location, | ||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| location = extract_location(_headers_from_exc(exc)) | ||||||||||||||||||||||||||||||||||
| if not location: | ||||||||||||||||||||||||||||||||||
| raise exc | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| headers: dict[str, str] = {} | ||||||||||||||||||||||||||||||||||
| if api_key: | ||||||||||||||||||||||||||||||||||
| headers["Authorization"] = f"Bearer {api_key}" | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| body, _status, _resp_headers = poll_location( | ||||||||||||||||||||||||||||||||||
| location, | ||||||||||||||||||||||||||||||||||
| headers=headers, | ||||||||||||||||||||||||||||||||||
| timeout=timeout, | ||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||
| return _parse_chat_completion(body) | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| def _patch_create(create_fn: Any, *, api_key: str | None, timeout: float) -> Any: | ||||||||||||||||||||||||||||||||||
| """Wrap an OpenAI ``create`` callable for VLM kwargs + Modal 303 polling. | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| ``skills``, ``toolsets``, and ``models`` are popped from ``kwargs`` and | ||||||||||||||||||||||||||||||||||
| merged into ``extra_body`` before the underlying call is made. | ||||||||||||||||||||||||||||||||||
| merged into ``extra_body`` before the underlying call is made. If the | ||||||||||||||||||||||||||||||||||
| gateway returns ``303 See Other`` (long request), the Location URL is | ||||||||||||||||||||||||||||||||||
| polled until the chat completion is ready. | ||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| @functools.wraps(create_fn) | ||||||||||||||||||||||||||||||||||
| def _create(*args: Any, **kwargs: Any) -> Any: | ||||||||||||||||||||||||||||||||||
| vlm_kwargs = {k: kwargs.pop(k) for k in _VLM_EXTRA_KEYS if k in kwargs} | ||||||||||||||||||||||||||||||||||
| if vlm_kwargs: | ||||||||||||||||||||||||||||||||||
| kwargs["extra_body"] = {**(kwargs.get("extra_body") or {}), **vlm_kwargs} | ||||||||||||||||||||||||||||||||||
| return create_fn(*args, **kwargs) | ||||||||||||||||||||||||||||||||||
| _pop_vlm_extra_kwargs(kwargs) | ||||||||||||||||||||||||||||||||||
| try: | ||||||||||||||||||||||||||||||||||
| return create_fn(*args, **kwargs) | ||||||||||||||||||||||||||||||||||
| except Exception as exc: | ||||||||||||||||||||||||||||||||||
| if _status_code_from_exc(exc) != 303: | ||||||||||||||||||||||||||||||||||
| raise | ||||||||||||||||||||||||||||||||||
| return _follow_long_request_303( | ||||||||||||||||||||||||||||||||||
| exc, | ||||||||||||||||||||||||||||||||||
| api_key=api_key, | ||||||||||||||||||||||||||||||||||
| timeout=max(float(timeout or 0), _DEFAULT_LONG_REQUEST_TIMEOUT), | ||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| return _create | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| def _patch_async_create(create_fn: Any) -> Any: | ||||||||||||||||||||||||||||||||||
| def _patch_async_create(create_fn: Any, *, api_key: str | None, timeout: float) -> Any: | ||||||||||||||||||||||||||||||||||
| """Async variant of :func:`_patch_create`.""" | ||||||||||||||||||||||||||||||||||
| import asyncio | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| @functools.wraps(create_fn) | ||||||||||||||||||||||||||||||||||
| async def _create(*args: Any, **kwargs: Any) -> Any: | ||||||||||||||||||||||||||||||||||
| vlm_kwargs = {k: kwargs.pop(k) for k in _VLM_EXTRA_KEYS if k in kwargs} | ||||||||||||||||||||||||||||||||||
| if vlm_kwargs: | ||||||||||||||||||||||||||||||||||
| kwargs["extra_body"] = {**(kwargs.get("extra_body") or {}), **vlm_kwargs} | ||||||||||||||||||||||||||||||||||
| return await create_fn(*args, **kwargs) | ||||||||||||||||||||||||||||||||||
| _pop_vlm_extra_kwargs(kwargs) | ||||||||||||||||||||||||||||||||||
| try: | ||||||||||||||||||||||||||||||||||
| return await create_fn(*args, **kwargs) | ||||||||||||||||||||||||||||||||||
| except Exception as exc: | ||||||||||||||||||||||||||||||||||
| if _status_code_from_exc(exc) != 303: | ||||||||||||||||||||||||||||||||||
| raise | ||||||||||||||||||||||||||||||||||
| return await asyncio.to_thread( | ||||||||||||||||||||||||||||||||||
| _follow_long_request_303, | ||||||||||||||||||||||||||||||||||
| exc, | ||||||||||||||||||||||||||||||||||
| api_key=api_key, | ||||||||||||||||||||||||||||||||||
| timeout=max(float(timeout or 0), _DEFAULT_LONG_REQUEST_TIMEOUT), | ||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+141
to
+146
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.
Suggested change
|
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| return _create | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
|
|
@@ -325,7 +416,11 @@ def completions(self): | |||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| completions = openai_client.chat.completions | ||||||||||||||||||||||||||||||||||
| completions.create = _patch_create(completions.create) | ||||||||||||||||||||||||||||||||||
| completions.create = _patch_create( | ||||||||||||||||||||||||||||||||||
| completions.create, | ||||||||||||||||||||||||||||||||||
| api_key=self._client.api_key, | ||||||||||||||||||||||||||||||||||
| timeout=float(self._client.timeout or _DEFAULT_LONG_REQUEST_TIMEOUT), | ||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||
| return completions | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| @cached_property | ||||||||||||||||||||||||||||||||||
|
|
@@ -379,5 +474,9 @@ async def main(): | |||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| completions = async_openai_client.chat.completions | ||||||||||||||||||||||||||||||||||
| completions.create = _patch_async_create(completions.create) | ||||||||||||||||||||||||||||||||||
| completions.create = _patch_async_create( | ||||||||||||||||||||||||||||||||||
| completions.create, | ||||||||||||||||||||||||||||||||||
| api_key=self._client.api_key, | ||||||||||||||||||||||||||||||||||
| timeout=float(self._client.timeout or _DEFAULT_LONG_REQUEST_TIMEOUT), | ||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||
| return completions | ||||||||||||||||||||||||||||||||||
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.
Relative redirect URLs in the
Locationheader will causerequeststo raise aMissingSchemaexception. To prevent this, resolve theLocationURL against the original request or response URL.