Repository navigation
fix: preserve provider tool-stop failures #811
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
Merged
seonghobae
merged 2 commits into
ContextualWisdomLab:fix/issue-567-tool-failure
from
seonghobae:repair/provider-tool-stop-v2203
Aug 21, 2026
Merged
Changes from all commits
Commits
Show all changes
2 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -423,7 +423,8 @@ def as_dict(self) -> dict[str, Any]: | |
|
|
||
| def _is_tool_execution_stopped(error: urllib.error.HTTPError) -> bool: | ||
| """Return whether an HTTP error carries the terminal tool-stop contract.""" | ||
| cached = getattr(error, "_contextual_tool_execution_stopped", None) | ||
| cache_key = "_contextual_orchestrator_tool_execution_stopped" | ||
| cached = getattr(error, cache_key, None) | ||
| if isinstance(cached, bool): | ||
| return cached | ||
| try: | ||
|
|
@@ -434,23 +435,23 @@ def _is_tool_execution_stopped(error: urllib.error.HTTPError) -> bool: | |
| details = payload.get("error") if isinstance(payload, dict) else None | ||
| result = isinstance(details, dict) and details.get("code") == "tool_execution_stopped" | ||
| try: | ||
| error._contextual_tool_execution_stopped = result | ||
| except AttributeError: # pragma: no cover - HTTPError permits attributes | ||
| setattr(error, cache_key, result) | ||
| except (AttributeError, TypeError): # pragma: no cover - HTTPError is mutable | ||
| pass | ||
| return result | ||
|
|
||
|
|
||
| def _provider_tool_execution_stopped(agent_id: str) -> ToolFallbackStoppedError: | ||
| """Convert the provider's terminal tool-stop response to the safe gateway contract.""" | ||
| def _provider_tool_execution_stopped(agent: ModelAgent) -> ToolFallbackStoppedError: | ||
| """Convert the provider's terminal tool-stop contract to the public safe error.""" | ||
| decision = classify_tool_failure( | ||
| ToolExecutionError( | ||
| "provider reported tool_execution_stopped", | ||
| tool_name="provider", | ||
| "provider reported terminal tool execution state", | ||
| tool_name="provider_tool_runtime", | ||
| kind=ToolFailureKind.TRANSPORT_ERROR, | ||
| outcome_unknown=True, | ||
| ) | ||
| ) | ||
| return ToolFallbackStoppedError(agent_id, decision) | ||
| return ToolFallbackStoppedError(agent.id, decision) | ||
|
|
||
|
|
||
| def _is_local_provider_url(base_url: str) -> bool: | ||
|
|
@@ -958,7 +959,7 @@ def _send_with_retry( | |
| break | ||
| self._sleep(self._backoff_delay(attempt)) | ||
| if isinstance(last_error, urllib.error.HTTPError) and _is_tool_execution_stopped(last_error): | ||
| raise _provider_tool_execution_stopped(agent.id) from None | ||
| raise _provider_tool_execution_stopped(agent) from None | ||
| if isinstance(last_error, ProviderResponseError): | ||
| raise last_error | ||
| raise RuntimeError(f"provider {agent.id} request failed") from None | ||
|
|
@@ -1154,21 +1155,26 @@ def _stream_send( | |
| headers=headers, | ||
| method="POST", | ||
| ) | ||
| with self._open_provider(request, destination) as response: | ||
| for raw in response: | ||
| line = raw.decode("utf-8").strip() | ||
| if not line.startswith("data:"): | ||
| continue | ||
| data = line[len("data:") :].strip() | ||
| if data == "[DONE]": | ||
| break | ||
| try: | ||
| chunk = json.loads(data) | ||
| except json.JSONDecodeError: | ||
| continue | ||
| delta = chunk.get("choices", [{}])[0].get("delta", {}).get("content") | ||
| if delta: | ||
| yield delta | ||
| try: | ||
| with self._open_provider(request, destination) as response: | ||
| for raw in response: | ||
| line = raw.decode("utf-8").strip() | ||
| if not line.startswith("data:"): | ||
| continue | ||
| data = line[len("data:") :].strip() | ||
| if data == "[DONE]": | ||
| break | ||
| try: | ||
| chunk = json.loads(data) | ||
| except json.JSONDecodeError: | ||
| continue | ||
| delta = chunk.get("choices", [{}])[0].get("delta", {}).get("content") | ||
| if delta: | ||
| yield delta | ||
| except urllib.error.HTTPError as exc: | ||
| if _is_tool_execution_stopped(exc): | ||
| raise _provider_tool_execution_stopped(agent) from None | ||
| raise | ||
|
Comment on lines
+1174
to
+1177
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. |
||
|
|
||
| # -- Full OpenAI passthrough (transport) ------------------------------------ | ||
| # Requests that carry provider features the multi-agent verifier cannot merge | ||
|
|
@@ -1215,7 +1221,7 @@ def _send_raw_with_retry( | |
| break | ||
| self._sleep(self._backoff_delay(attempt)) | ||
| if isinstance(last_error, urllib.error.HTTPError) and _is_tool_execution_stopped(last_error): | ||
| raise _provider_tool_execution_stopped(agent.id) from None | ||
| raise _provider_tool_execution_stopped(agent) from None | ||
| raise RuntimeError(f"provider {agent.id} passthrough request failed") from None | ||
|
|
||
| def _send_raw( | ||
|
|
@@ -2617,7 +2623,7 @@ def _invoke( | |
| while True: | ||
| try: | ||
| output = self.client.chat(agent, messages) | ||
| except Exception as exc: # noqa: BLE001 - classify before fallback | ||
| except Exception as exc: | ||
| if isinstance(exc, (ProviderResponseError, ToolFallbackStoppedError)): | ||
| raise | ||
| last_error = exc | ||
|
|
||
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
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.