Your lightweight open-source personal agent.
Python agent-frameworkai-agentai-agentschatbotchatopsdiscord-botllm-agentsllmslocal-llmmcpmodel-context-protocolmulti-agentopenai-compatibleopenclawpersonal-ai-assistantpythonself-hostedtelegram-bot-ai-assistantwebuiworkflow-automation
fix(providers): tighten tool calls and compatibility fallbacks
chengyongru8b37976d36c2
diff --git a/docs/configuration.md b/docs/configuration.mdindex 95bcf3380..168c1045e 100644--- a/docs/configuration.md+++ b/docs/configuration.md@@ -419,6 +419,8 @@ Providers that use the Responses API can keep reasoning context across a conversnanobot preserves Responses conversation state automatically for OpenAI Responses, OpenAI Codex, Azure OpenAI, DeepSeek V4, and compatible GitHub Copilot models. Native compaction is also automatic when the provider supports it. The threshold is derived from the active model's context window and reserved output headroom; no provider configuration is required.+An explicit rejection of a native compaction field or input type disables that capability for the current provider instance. Invalid threshold values and other request errors do not disable it. For OpenAI and Azure Responses requests, an unsupported compaction field permits one retry without that field; a validation error is returned for correction. Codex compaction required to fit the input budget must succeed before the conversation can continue.+<details><summary><b>Azure OpenAI</b></summary>@@ -1508,7 +1510,7 @@ To configure presets in `config.json`:`supportedApis` must contain at least one of `chat_completions`, `responses`, or `anthropic_messages`. `preferredApi` must belong to that list and defaults to its first entry. Omit `api` or set it to `null` for Auto. An explicit preset declaration overrides the OpenAI connection default; for custom connections, it must stay within the connection's supported APIs.-A Responses-only preset never falls back to Chat Completions. To allow Chat fallback for Responses compatibility errors, include both `responses` and `chat_completions` with `preferredApi: "responses"`. Other errors do not trigger this API fallback. Provider-hosted search uses Responses when the preset allows it.+A Responses-only preset never falls back to Chat Completions. To allow Chat fallback for Responses compatibility errors, include both `responses` and `chat_completions` with `preferredApi: "responses"`. Fallback requires an explicit rejection of the Responses API, a Responses-specific parameter, or the `input_image` input type. Invalid parameter values, missing conversation state, authentication failures, rate limits, and server errors do not trigger this API fallback or count toward temporarily bypassing Responses. Provider-hosted search uses Responses when the preset allows it.Declare `anthropic_messages` alone in a preset. Requests use the same provider connection settings and go to `/v1/messages`; `apiBase` may include a trailing `/v1`. Automatic API fallback between Anthropic Messages and OpenAI formats is not supported; use separate [fallback presets](#model-fallbacks) when needed.diff --git a/docs/providers.md b/docs/providers.mdindex 645342673..fb2e22a00 100644--- a/docs/providers.md+++ b/docs/providers.md@@ -78,6 +78,10 @@ You usually omit `apiBase` for hosted built-in providers such as OpenRouter, AntUse `proxy` when one provider must send HTTP traffic through a proxy without changing process-wide `HTTP_PROXY` / `HTTPS_PROXY`. Supported providers include `openai`, `custom`, named custom providers, OpenRouter-style gateways, local OpenAI-compatible servers, Anthropic Messages, `openai_codex`, and `xai_grok`. For OAuth providers, this also covers exchanging and refreshing tokens. `bedrock`, `azure_openai`, and `github_copilot` reject `proxy`; use their endpoint-specific configuration instead.+Tool execution requires the API's structured tool-call fields. OpenAI-compatible services that put `<tool_call>` markup in assistant text must expose structured tool calls to use nanobot's tools; nanobot preserves that markup as text.++If a reply contains raw `<function=...>` or `<parameter=...>` tags, check the model and serving endpoint's tool-calling support. For self-hosted models, configure the server's tool-call parser for that model. For hosted services, ask the provider to return structured `tool_calls`, or use a compatible model and endpoint. Code examples remain text and are not executed. Changing the chat channel or hiding tags does not repair an upstream tool-calling format error.+## Inline Image RequestsBefore sending a large inline image batch, providers prepare smaller copies while keeping the attachment files intact. This applies to Responses, Chat Completions, Anthropic Messages, and Bedrock Converse requests, including images returned by tools and images in Responses history replay. Preparation first re-encodes at the original dimensions, then reduces each dimension by at most 25% if needed, with JPEG quality at least 65. PNG transparency is preserved. The shared 1 MB image data URL budget is a best-effort transport target; small image blocks, animated images, and remote references keep their original payloads. Gateway logs report image sizes and byte counts.diff --git a/nanobot/providers/openai_codex_provider.py b/nanobot/providers/openai_codex_provider.pyindex 2fea8cee9..35417dda5 100644--- a/nanobot/providers/openai_codex_provider.py+++ b/nanobot/providers/openai_codex_provider.py@@ -41,6 +41,10 @@ from nanobot.providers.openai_responses import (ResponsesWebSocketOptions,responses_state_matches,)+from nanobot.providers.openai_responses.errors import (+ COMPACTION_FEATURES,+ is_unsupported_feature_error,+)from nanobot.providers.openai_responses.state import (responses_compaction_items,without_response_item_ids,@@ -620,10 +624,7 @@ def _build_codex_http_error(status_code: int, headers: httpx.Headers, raw: str)retry_after = LLMProvider._extract_retry_after_from_headers(headers)error_type, error_code = LLMProvider._extract_error_type_code(raw)error_param, error_message = _codex_error_details(raw)- compaction_unsupported = status_code in {400, 404, 422} and any(- marker in raw.lower()- for marker in ("context_management", "compact_threshold", "compaction_trigger")- )+ compaction_unsupported = is_unsupported_feature_error(status_code, raw, COMPACTION_FEATURES)return _CodexHTTPError(_friendly_error(status_code), status_code=status_code,retry_after=retry_after, error_type=error_type, error_code=error_code,diff --git a/nanobot/providers/openai_compat_provider.py b/nanobot/providers/openai_compat_provider.pyindex 19d8803b8..e5d917435 100644--- a/nanobot/providers/openai_compat_provider.py+++ b/nanobot/providers/openai_compat_provider.py@@ -7,7 +7,6 @@ import hashlibimport importlib.utilimport jsonimport os-import reimport secretsimport stringimport time@@ -40,6 +39,11 @@ from nanobot.providers.model_api import (is_hosted_web_search_tool,)from nanobot.providers.openai_responses import ResponsesBackend, responses_state_matches+from nanobot.providers.openai_responses.errors import (+ RESPONSES_FEATURES,+ is_unsupported_feature_error,+ response_error_details,+)from nanobot.providers.registry import KIMI_K3_MODELSif TYPE_CHECKING:@@ -95,7 +99,6 @@ _KIMI_SERVER_MANAGED_TEMPERATURE_MODELS: frozenset[str] = frozenset({"kimi-k2.5","kimi-k2.6",})-_TEXT_TOOL_CALL_RE = re.compile(r"<tool_call>\s*(.*?)\s*</tool_call>", re.DOTALL)# Thinking-capable MiMo models per Xiaomi docs (see# tests/providers/test_xiaomi_mimo_thinking.py). mimo-v2-flash is omitted# because it does not support thinking.@@ -207,68 +210,6 @@ def _short_tool_id() -> str:return "".join(secrets.choice(_ALNUM) for _ in range(9))-def _strip_json_fence(text: str) -> str:- stripped = text.strip()- if not stripped.startswith("```") or not stripped.endswith("```"):- return stripped- lines = stripped.splitlines()- if len(lines) < 2:- return stripped- return "\n".join(lines[1:-1]).strip()---def _extract_text_tool_calls(content: str | None) -> tuple[str | None, list[ToolCallRequest]]:- if not content or "<tool_call>" not in content:- return content, []-- tool_calls: list[ToolCallRequest] = []- spans: list[tuple[int, int]] = []- for match in _TEXT_TOOL_CALL_RE.finditer(content):- try:- raw_payload: object = json.loads(- _strip_json_fence(match.group(1))- )- except Exception:- continue- if not isinstance(raw_payload, dict):- continue- payload = cast(dict[str, Any], raw_payload)-- nested = cast(object, payload.get("tool_call"))- if isinstance(nested, dict):- payload = cast(dict[str, Any], nested)- function = cast(object, payload.get("function"))- if not isinstance(function, dict):- function = payload- function_data = cast(dict[str, Any], function)- name = cast(object, function_data.get("name"))- if not isinstance(name, str) or not name:- continue-- arguments = function_data.get(- "arguments",- payload.get("arguments", {}),- )- tool_calls.append(ToolCallRequest(- id=str(payload.get("id") or _short_tool_id()),- name=name,- arguments=parse_tool_arguments(arguments),- ))- spans.append(match.span())-- if not tool_calls:- return content, []-- visible_parts: list[str] = []- last = 0- for start, end in spans:- visible_parts.append(content[last:start])- last = end- visible_parts.append(content[last:])- visible_content = "".join(visible_parts).strip() or None- return visible_content, tool_calls--def _get(obj: object, key: str) -> Any:if isinstance(obj, dict):return cast(dict[str, Any], obj).get(key)@@ -1146,32 +1087,9 @@ class OpenAICompatProvider(LLMProvider):@staticmethoddef _should_fallback_from_responses_error(e: Exception) -> bool:- """Fallback only for likely Responses API compatibility errors."""- response = getattr(e, "response", None)- status_code = getattr(e, "status_code", None)- if status_code is None and response is not None:- status_code = getattr(response, "status_code", None)- if status_code not in {400, 404, 422}:- return False-- body = (- getattr(e, "body", None)- or getattr(e, "doc", None)- or getattr(response, "text", None)- )- body_text = str(body).lower() if body is not None else ""- compatibility_markers = (- "responses",- "response api",- "max_output_tokens",- "instructions",- "previous_response",- "unsupported",- "not supported",- "unknown parameter",- "unrecognized request argument",- )- return any(marker in body_text for marker in compatibility_markers)+ """Fallback only when the endpoint rejects a Responses API feature."""+ status, body = response_error_details(e)+ return is_unsupported_feature_error(status, body, RESPONSES_FEATURES)def _build_responses_body(self,@@ -1494,8 +1412,6 @@ class OpenAICompatProvider(LLMProvider):provider_specific_fields=prov,function_provider_specific_fields=fn_prov,))- if not parsed_tool_calls:- content, parsed_tool_calls = _extract_text_tool_calls(content)return LLMResponse(content=content,@@ -1620,8 +1536,6 @@ class OpenAICompatProvider(LLMProvider):)for b in tc_bufs.values()]- if not tool_calls:- content, tool_calls = _extract_text_tool_calls(content)return LLMResponse(content=content,diff --git a/nanobot/providers/openai_responses/backend.py b/nanobot/providers/openai_responses/backend.pyindex 8b32a6a47..69b759e0d 100644--- a/nanobot/providers/openai_responses/backend.py+++ b/nanobot/providers/openai_responses/backend.py@@ -271,13 +271,18 @@ class ResponsesBackend:idle_timeout = resolve_stream_idle_timeout_s()if body.get("stream"):options["timeout"] = idle_timeout+ # Older supported SDKs accept server compaction through extra_body.+ sdk_body = dict(body)+ if "context_management" in sdk_body:+ options["extra_body"] = {"context_management": sdk_body.pop("context_management")}try:- response = await client.responses.create(**body, **options)+ response = await client.responses.create(**sdk_body, **options)except Exception as exc:if "context_management" not in body or not is_compaction_compatibility_error(exc):raiseself.native_compaction_available = Falsebody.pop("context_management")+ options.pop("extra_body")logger.warning("Responses server compaction unsupported; disabled for {} (status={})",provider, getattr(exc, "status_code", None),diff --git a/nanobot/providers/openai_responses/errors.py b/nanobot/providers/openai_responses/errors.pynew file mode 100644index 000000000..65196ec4d--- /dev/null+++ b/nanobot/providers/openai_responses/errors.py@@ -0,0 +1,62 @@+"""Explicit unsupported-feature errors at the Responses transport boundary."""++from __future__ import annotations++import json+import re+from typing import cast++COMPACTION_FEATURES = ("context_management", "compact_threshold", "compaction_trigger")+RESPONSES_FEATURES = (+ "responses", "responses api", "responses endpoint", "response api",+ "max_output_tokens", "instructions", "previous_response_id", "input_image",+)+++def response_error_details(exc: Exception) -> tuple[object, object]:+ """Read SDK error metadata without treating local exceptions as API errors."""+ response = getattr(exc, "response", None)+ status = getattr(exc, "status_code", None) or getattr(response, "status_code", None)+ body = (+ getattr(exc, "body", None)+ or getattr(exc, "doc", None)+ or getattr(response, "text", None)+ )+ return status, body+++def is_unsupported_feature_error(+ status: object, body: object, features: tuple[str, ...],+) -> bool:+ """Require rejection of a named feature, not an invalid value or incidental mention."""+ if status not in (400, 404, 422):+ return False+ if isinstance(body, str):+ try:+ body = json.loads(body)+ except ValueError:+ pass+ if isinstance(body, dict):+ fields = cast(dict[str, object], body)+ error = fields.get("error")+ if isinstance(error, dict):+ fields = cast(dict[str, object], error)+ code = fields.get("code")+ param = fields.get("param")+ if code in ("unknown_parameter", "unsupported_parameter", "unrecognized_parameter"):+ if isinstance(param, str) and re.split(r"[.\[]", param)[0] in features:+ return True+ if code in ("invalid_value", "unsupported_value"):+ return False+ body = fields.get("message")+ if not isinstance(body, str):+ return False+ target = "(?:" + "|".join(re.escape(feature) for feature in features) + ")"+ return bool(re.search(+ rf"(?:unknown|unrecognized|unsupported)\s+"+ rf"(?:parameter|field|request argument(?: supplied)?|input type|endpoint|api)"+ rf"\s*[:=]?\s*[`'\"]?{target}(?!\w)"+ rf"|(?<!\w){target}[`'\"]?\s+(?:is\s+)?(?:not supported|unsupported|not implemented)\b"+ rf"|does not support\s+(?:the\s+)?{target}(?!\w)",+ body, re.IGNORECASE,+ ))diff --git a/nanobot/providers/openai_responses/state.py b/nanobot/providers/openai_responses/state.pyindex d0c6a5d8a..861927f9a 100644--- a/nanobot/providers/openai_responses/state.py+++ b/nanobot/providers/openai_responses/state.py@@ -7,6 +7,11 @@ from loguru import loggerfrom nanobot.providers.base import LLMResponse, LLMUsage, ProviderConversationStatefrom nanobot.providers.openai_responses.converters import convert_messages+from nanobot.providers.openai_responses.errors import (+ COMPACTION_FEATURES,+ is_unsupported_feature_error,+ response_error_details,+)RESPONSES_STATE_KIND = "openai_responses"RESPONSES_STATE_VERSION = 1@@ -193,24 +198,8 @@ def is_compaction_compatibility_error(exc: Exception) -> bool:"""Recognize endpoints that reject native Responses compaction fields."""if getattr(exc, "compaction_unsupported", False) is True:return True- response = getattr(exc, "response", None)- status_code = getattr(exc, "status_code", None)- if status_code is None and response is not None:- status_code = getattr(response, "status_code", None)- body = (- getattr(exc, "body", None)- or getattr(exc, "doc", None)- or getattr(response, "text", None)- or str(exc)- )- text = str(body).lower()- has_compaction_marker = any(- marker in text- for marker in ("context_management", "compact_threshold", "compaction_trigger")- )- if not has_compaction_marker:- return False- return isinstance(exc, TypeError) or status_code in {400, 404, 422}+ status, body = response_error_details(exc)+ return is_unsupported_feature_error(status, body, COMPACTION_FEATURES)def _prune_before_latest_output_compaction(diff --git a/nanobot/providers/openai_responses/websocket.py b/nanobot/providers/openai_responses/websocket.pyindex c9b177e47..00fa9b2a7 100644--- a/nanobot/providers/openai_responses/websocket.py+++ b/nanobot/providers/openai_responses/websocket.py@@ -18,6 +18,10 @@ from websockets.protocol import Statefrom nanobot.providers.base import LLMProvider, LLMResponse, resolve_stream_idle_timeout_sfrom nanobot.providers.openai_responses.backend import ResponsesBackend+from nanobot.providers.openai_responses.errors import (+ COMPACTION_FEATURES,+ is_unsupported_feature_error,+)from nanobot.providers.openai_responses.parsing import ResponsesStreamCapture_TRANSPORT_FAILURE_LIMIT = 3@@ -88,9 +92,8 @@ class ResponsesWebSocketError(RuntimeError):_error_token(cast(dict[str, Any], raw_headers).get("x-request-id"))if isinstance(raw_headers, dict) else None)- self.compaction_unsupported = self.status_code in {400, 404, 422} and any(- marker in str(message).lower()- for marker in ("context_management", "compact_threshold", "compaction_trigger")+ self.compaction_unsupported = is_unsupported_feature_error(+ self.status_code, fields, COMPACTION_FEATURES,)super().__init__(f"HTTP {self.status_code}: Responses WebSocket request failed"diff --git a/tests/providers/test_azure_openai_provider.py b/tests/providers/test_azure_openai_provider.pyindex 596cf2ff5..4189a9ce3 100644--- a/tests/providers/test_azure_openai_provider.py+++ b/tests/providers/test_azure_openai_provider.py@@ -393,7 +393,7 @@ async def test_chat_retries_without_unsupported_server_compaction():assert result.content == "compaction fallback"assert result.provider_state is not Noneassert create.await_count == 2- assert "context_management" in create.call_args_list[0].kwargs+ assert "context_management" in create.call_args_list[0].kwargs["extra_body"]assert "context_management" not in create.call_args_list[1].kwargsassert provider.supports_native_compaction() is Falsediff --git a/tests/providers/test_custom_provider.py b/tests/providers/test_custom_provider.pyindex 9378d7834..dc6664548 100644--- a/tests/providers/test_custom_provider.py+++ b/tests/providers/test_custom_provider.py@@ -52,29 +52,6 @@ def test_custom_provider_parse_accepts_dict_response() -> None:assert result.usage.total_tokens == 3-def test_custom_provider_parse_normalizes_text_tool_call() -> None:- with patch("nanobot.providers.openai_compat_provider.AsyncOpenAI"):- provider = OpenAICompatProvider()-- result = provider._parse({- "choices": [{- "message": {- "content": (- "I'll inspect it.\n"- '<tool_call>{"name":"read_file","arguments":{"path":"README.md"}}'- "</tool_call>"- ),- },- "finish_reason": "stop",- }],- })-- assert result.content == "I'll inspect it."- assert len(result.tool_calls) == 1- assert result.tool_calls[0].name == "read_file"- assert result.tool_calls[0].arguments == {"path": "README.md"}--def test_custom_provider_parse_keeps_structured_tool_call_over_text_markup() -> None:with patch("nanobot.providers.openai_compat_provider.AsyncOpenAI"):provider = OpenAICompatProvider()@@ -104,22 +81,6 @@ def test_custom_provider_parse_chunks_accepts_plain_text_chunks() -> None:assert result.content == "hello world"-def test_custom_provider_parse_chunks_normalizes_split_text_tool_call() -> None:- chunks = [- {"choices": [{"delta": {"content": "<tool_call>"}}]},- {"choices": [{"delta": {"content": '{"name":"list_dir",'}}]},- {"choices": [{"delta": {"content": '"arguments":{"path":"."}}</tool_call>'}}]},- {"choices": [{"finish_reason": "stop", "delta": {}}]},- ]-- result = OpenAICompatProvider._parse_chunks(chunks)-- assert result.content is None- assert len(result.tool_calls) == 1- assert result.tool_calls[0].name == "list_dir"- assert result.tool_calls[0].arguments == {"path": "."}--@pytest.mark.parametrize("as_sdk", [False, True], ids=["dict", "sdk"])def test_custom_provider_parse_chunks_deduplicates_parallel_tool_call_ids(as_sdk: bool) -> None:chunks = [{diff --git a/tests/providers/test_openai_codex_provider.py b/tests/providers/test_openai_codex_provider.pyindex 88fdbc400..f43d56f53 100644--- a/tests/providers/test_openai_codex_provider.py+++ b/tests/providers/test_openai_codex_provider.py@@ -313,8 +313,9 @@ async def test_codex_request_non_200_populates_http_metadata(monkeypatch) -> Non@pytest.mark.asyncio[email protected]("unsupported", [False, True])async def test_codex_request_marks_rejected_compaction_without_retaining_raw_body(- monkeypatch,+ monkeypatch, unsupported,) -> None:original_client = httpx.AsyncClientsecret = "PRIVATE PROMPT MUST NOT BE RETAINED"@@ -324,7 +325,10 @@ async def test_codex_request_marks_rejected_compaction_without_retaining_raw_bod400,json={"error": {- "message": f"Unknown input type compaction_trigger; {secret}",+ "message": (+ f"Unknown input type compaction_trigger; {secret}" if unsupported+ else f"Invalid compaction_trigger: input exceeds context window; {secret}"+ ),},},request=request,@@ -349,7 +353,7 @@ async def test_codex_request_marks_rejected_compaction_without_retaining_raw_bod)error = caught.value- assert error.compaction_unsupported is True+ assert error.compaction_unsupported is unsupportedassert secret not in str(error)assert not hasattr(error, "body")diff --git a/tests/providers/test_openai_codex_websocket.py b/tests/providers/test_openai_codex_websocket.pyindex 6e8b0a114..e7cb7a0e8 100644--- a/tests/providers/test_openai_codex_websocket.py+++ b/tests/providers/test_openai_codex_websocket.py@@ -764,6 +764,31 @@ async def test_codex_compaction_restarts_continuation_from_rewritten_history(codassert any(item.get("type") == "compaction" for item in server.requests[2][1]["input"])[email protected]("unsupported", [False, True])+async def test_required_compaction_error_preserves_capability_unless_unsupported(codex_peer, unsupported):+ provider, server, _ = codex_peer+ first = await provider.chat(_messages(), provider_context=_context())++ async def respond(peer, _body):+ await peer.socket.send_json({"type": "error", "status": 400, "error": {+ "message": (+ "Unknown input type compaction_trigger" if unsupported+ else "Invalid compaction_trigger: input exceeds context window"+ ),+ }})++ server.respond = respond+ result = await provider.chat(+ _messages("next"), max_tokens=10,+ provider_context=_context(first, context_window_tokens=4_000, compaction_input_budget=10_000),+ )+ assert result.finish_reason == "error"+ assert provider.supports_native_compaction() is (not unsupported)+ assert len(server.requests) == 2+ assert server.requests[-1][1]["input"] == [{"type": "compaction_trigger"}]+ assert not server.http_requests++async def test_codex_inline_compaction_starts_next_request_with_compacted_state(codex_peer):provider, server, _ = codex_peercompaction = {"type": "compaction", "id": "cmp_1", "encrypted_content": "compacted"}diff --git a/tests/providers/test_openai_compat_spec.py b/tests/providers/test_openai_compat_spec.pyindex 3f1a75465..b8600348c 100644--- a/tests/providers/test_openai_compat_spec.py+++ b/tests/providers/test_openai_compat_spec.py@@ -689,7 +689,7 @@ async def test_direct_openai_retries_without_unsupported_server_compaction() ->assert result.content == "compaction fallback"assert result.provider_state is not Noneassert mock_responses.await_count == 2- assert "context_management" in mock_responses.call_args_list[0].kwargs+ assert "context_management" in mock_responses.call_args_list[0].kwargs["extra_body"]assert "context_management" not in mock_responses.call_args_list[1].kwargsassert provider.supports_native_compaction("gpt-5.6") is Falsemock_chat.assert_not_awaited()diff --git a/tests/providers/test_openai_responses.py b/tests/providers/test_openai_responses.pyindex 3fb069905..49437d3c9 100644--- a/tests/providers/test_openai_responses.py+++ b/tests/providers/test_openai_responses.py@@ -818,9 +818,9 @@ class TestResponsesConversationState:):assert resolve_compact_threshold(context_window, max_output) == expected- def test_compaction_compatibility_recognizes_old_sdk_signature_error(self):+ def test_local_sdk_errors_do_not_disable_server_compaction(self):error = TypeError("create() got an unexpected keyword argument 'context_management'")- assert is_compaction_compatibility_error(error) is True+ assert is_compaction_compatibility_error(error) is Falseassert is_compaction_compatibility_error(TypeError("unrelated argument")) is Falsedef test_state_observability_logs_counts_without_opaque_content(self):diff --git a/tests/providers/test_provider_compatibility.py b/tests/providers/test_provider_compatibility.pynew file mode 100644index 000000000..751fc8522--- /dev/null+++ b/tests/providers/test_provider_compatibility.py@@ -0,0 +1,257 @@+"""Provider compatibility decisions through the SDK and local HTTP endpoints."""++import json++import pytest+from aiohttp import web++from nanobot.agent.tools.exec_session import ExecSessionTool+from nanobot.providers.azure_openai_provider import AzureOpenAIProvider+from nanobot.providers.base import ProviderCallContext+from nanobot.providers.model_api import ModelAPICapabilities+from nanobot.providers.openai_compat_provider import OpenAICompatProvider+from nanobot.providers.registry import find_by_name+++async def endpoint(monkeypatch):+ monkeypatch.setenv("NO_PROXY", "127.0.0.1")+ monkeypatch.setenv("no_proxy", "127.0.0.1")+ monkeypatch.delenv("LANGFUSE_SECRET_KEY", raising=False)+ runners = []++ async def start(handler):+ app = web.Application()+ app.router.add_post("/{path:.*}", handler)+ runner = web.AppRunner(app)+ runners.append(runner)+ await runner.setup()+ await web.TCPSite(runner, "127.0.0.1", 0).start()+ return f"http://127.0.0.1:{runner.addresses[0][1]}"++ yield start+ for runner in runners:+ await runner.cleanup()+++def _response(body):+ response = {+ "id": "resp_fixture", "object": "response", "status": "completed",+ "output": [{+ "id": "msg_fixture", "type": "message", "role": "assistant", "status": "completed",+ "content": [{"type": "output_text", "text": "ok", "annotations": []}],+ }],+ }+ if not body.get("stream"):+ return web.json_response(response)+ return _sse([+ {"type": "response.output_text.delta", "delta": "ok"},+ {"type": "response.completed", "response": response},+ ])+++def _sse(events):+ return web.Response(+ content_type="text/event-stream",+ text="".join(f"data: {json.dumps(event)}\n\n" for event in events),+ )++[email protected]("stream", [False, True])[email protected](("content", "tool_choice"), [+ pytest.param(+ 'Example only:\n```xml\n<tool_call>{"name":"read_file","arguments":{"path":"README.md"}}</tool_call>\n```',+ "none", id="quoted-json-tools-disabled",+ ),+ pytest.param(+ 'Example only:\n```xml\n<tool_call>{"name":"read_file","arguments":{"path":"README.md"}}</tool_call>\n```',+ "auto", id="quoted-json-tools-enabled",+ ),+ pytest.param(+ '<tool_call><function=exec_session><parameter=session_id>fixture</parameter>'+ '<parameter=timeout_ms>{"session_id":"fixture","timeout_ms":{}}</parameter>'+ '</function></tool_call>',+ "auto", id="malformed-xml-in-content",+ ),+])+async def test_plaintext_tool_examples_remain_text(endpoint, stream, content, tool_choice):+ requests = []+ deltas = []++ async def on_content_delta(delta):+ deltas.append(delta)++ async def handler(request):+ body = await request.json()+ requests.append(body)+ if body.get("stream"):+ return _sse([+ {"choices": [{"index": 0, "delta": {"content": part}}]}+ for part in (content[:30], content[30:35], content[35:])+ ] + [{"choices": [{"index": 0, "delta": {}, "finish_reason": "stop"}]}])+ return web.json_response({"id": "chat_fixture", "choices": [{+ "index": 0, "message": {"role": "assistant", "content": content}, "finish_reason": "stop",+ }]})++ url = await endpoint(handler)+ provider = OpenAICompatProvider(api_key="fixture", api_base=f"{url}/v1", spec=find_by_name("custom"))+ invoke = provider.chat_stream if stream else provider.chat+ try:+ result = await invoke(+ [{"role": "user", "content": "Show a tool call example without executing it."}],+ tools=[{"type": "function", "function": {"name": "read_file", "parameters": {"type": "object"}}}],+ tool_choice=tool_choice,+ **({"on_content_delta": on_content_delta} if stream else {}),+ )+ assert result.finish_reason == "stop"+ assert result.content == content+ assert result.tool_calls == []+ assert not result.should_execute_tools+ assert len(requests) == 1+ assert requests[0]["tool_choice"] == tool_choice+ if stream:+ assert "".join(deltas) == result.content+ finally:+ await provider.aclose()++[email protected]("stream", [False, True])[email protected]("timeout_ms", [1000, {"session_id": "fixture", "timeout_ms": {}}])+async def test_structured_calls_keep_arguments_out_of_text_and_preserve_validation(endpoint, stream, timeout_ms):+ tool = ExecSessionTool()+ arguments = {"session_id": "fixture", "timeout_ms": timeout_ms}+ encoded = json.dumps(arguments)+ text = "Checking the command output."+ deltas = []++ async def on_content_delta(delta):+ deltas.append(delta)++ async def handler(request):+ body = await request.json()+ if body.get("stream"):+ return _sse([+ {"choices": [{"index": 0, "delta": {"content": text}}]},+ {"choices": [{"index": 0, "delta": {"tool_calls": [{+ "index": 0, "id": "call_fixture", "type": "function",+ "function": {"name": "exec_session", "arguments": encoded[:20]},+ }]}}]},+ {"choices": [{"index": 0, "delta": {"tool_calls": [{+ "index": 0, "function": {"arguments": encoded[20:]},+ }]}}]},+ {"choices": [{"index": 0, "delta": {}, "finish_reason": "tool_calls"}]},+ ])+ return web.json_response({"id": "chat_fixture", "choices": [{+ "index": 0, "message": {"role": "assistant", "content": text, "tool_calls": [{+ "id": "call_fixture", "type": "function",+ "function": {"name": "exec_session", "arguments": encoded},+ }]}, "finish_reason": "tool_calls",+ }]})++ url = await endpoint(handler)+ provider = OpenAICompatProvider(api_key="fixture", api_base=f"{url}/v1", spec=find_by_name("custom"))+ invoke = provider.chat_stream if stream else provider.chat+ try:+ result = await invoke(+ [{"role": "user", "content": "Check the running command."}],+ tools=[tool.to_schema()],+ **({"on_content_delta": on_content_delta} if stream else {}),+ )+ assert result.should_execute_tools+ assert result.content == text+ assert len(result.tool_calls) == 1+ call = result.tool_calls[0]+ assert (call.id, call.name, call.arguments) == ("call_fixture", "exec_session", arguments)+ assert tool.validate_params(call.arguments) == (+ ["timeout_ms should be integer"] if isinstance(timeout_ms, dict) else []+ )+ if stream:+ assert "".join(deltas) == text+ finally:+ await provider.aclose()++[email protected]("stream", [False, True])+async def test_invalid_output_limit_does_not_switch_protocol_or_trip_circuit(endpoint, stream):+ paths = []++ async def handler(request):+ paths.append(request.path)+ body = await request.json()+ if body.get("max_output_tokens", body.get("max_completion_tokens", 0)) > 64:+ return web.json_response({"error": {+ "type": "invalid_request_error", "param": "max_output_tokens",+ "message": "Invalid max_output_tokens for Responses: must be at most 64",+ }}, status=400)+ return _response(body)++ url = await endpoint(handler)+ provider = OpenAICompatProvider(+ api_key="fixture", api_base=f"{url}/v1", default_model="gpt-5", spec=find_by_name("custom"),+ model_api=ModelAPICapabilities(("responses", "chat_completions"), "responses"),+ )+ invoke = provider.chat_stream if stream else provider.chat+ try:+ for _ in range(3):+ result = await invoke([{"role": "user", "content": "hello"}], max_tokens=100)+ assert result.finish_reason == "error"+ assert result.error_status_code == 400+ result = await invoke([{"role": "user", "content": "hello"}], max_tokens=32)+ assert result.content == "ok"+ assert paths == ["/v1/responses"] * 4+ finally:+ await provider.aclose()++[email protected]("stream", [False, True])[email protected]("unsupported", [False, True])+async def test_server_compaction_distinguishes_unsupported_field_from_invalid_value(endpoint, stream, unsupported):+ bodies = []++ async def handler(request):+ body = await request.json()+ bodies.append(body)+ if len(bodies) == 1:+ return web.json_response({"error": {+ "type": "invalid_request_error", "param": "context_management[0].compact_threshold",+ "message": (+ "Unknown parameter: context_management" if unsupported+ else "Invalid context_management compact_threshold: value exceeds the context window"+ ),+ }}, status=400)+ return _response(body)++ url = await endpoint(handler)+ provider = AzureOpenAIProvider(api_key="fixture", api_base=url, default_model="gpt-5.4")+ invoke = provider.chat_stream if stream else provider.chat+ context = ProviderCallContext(context_window_tokens=200_000)+ tools = [ExecSessionTool().to_schema()]+ messages = [+ {"role": "system", "content": "Stable instructions."},+ {"role": "user", "content": "Check the running command."},+ {"role": "assistant", "content": None, "tool_calls": [{+ "id": "call_fixture", "type": "function", "function": {+ "name": "exec_session", "arguments": '{"session_id":"fixture"}',+ },+ }]},+ {"role": "tool", "tool_call_id": "call_fixture", "content": "Command completed."},+ ]+ try:+ result = await invoke(messages, tools=tools, provider_context=context)+ assert result.finish_reason == ("stop" if unsupported else "error")+ assert len(bodies) == (2 if unsupported else 1)+ assert provider.supports_native_compaction() is (not unsupported)+ if unsupported:+ without_compaction = {key: value for key, value in bodies[0].items() if key != "context_management"}+ assert json.dumps(bodies[1]) == json.dumps(without_compaction)+ result = await invoke(messages, tools=tools, provider_context=context)+ assert result.content == "ok"+ assert "context_management" in bodies[0]+ assert ("context_management" in bodies[-1]) is (not unsupported)+ assert json.dumps(bodies[-1]) == json.dumps(bodies[-2] if unsupported else bodies[0])+ assert bodies[-1]["instructions"] == "Stable instructions."+ assert bodies[-1]["input"][0]["role"] == "user"+ assert [item["type"] for item in bodies[-1]["input"][1:]] == ["function_call", "function_call_output"]+ assert bodies[-1]["tools"][0]["name"] == "exec_session"+ finally:+ await provider.aclose()diff --git a/tests/providers/test_responses_circuit_breaker.py b/tests/providers/test_responses_circuit_breaker.pyindex 557c50fdc..d48586247 100644--- a/tests/providers/test_responses_circuit_breaker.py+++ b/tests/providers/test_responses_circuit_breaker.py@@ -201,11 +201,22 @@ def test_serde_deserialize_error_does_not_trigger_fallback():assert OpenAICompatProvider._should_fallback_from_responses_error(err) is False-def test_legacy_compatibility_markers_still_trigger_fallback():+def test_explicit_unsupported_responses_parameter_allows_fallback():err = _FakeAPIError(400, "parameter `instructions` is unsupported")assert OpenAICompatProvider._should_fallback_from_responses_error(err) is True[email protected](("status", "body", "expected"), [+ (400, {"code": "unsupported_parameter", "param": "instructions"}, True),+ (400, {"code": "unsupported_value", "param": "instructions", "message": "Invalid instructions"}, False),+ (404, {"message": "Previous response with id 'resp_missing' not found"}, False),+ (422, {"message": "Unsupported value for max_output_tokens: must be at most 64"}, False),+])+def test_responses_errors_distinguish_unsupported_parameters_from_invalid_requests(status, body, expected):+ err = _FakeAPIError(status, body)+ assert OpenAICompatProvider._should_fallback_from_responses_error(err) is expected++# DeepSeek Responses wire shape (PR #5214 root cause)