"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(providers): a chat-completions refusal that names /v1/responses is healed there (#1473)
Haozhe Wu60ef024ba3fa
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 3cdc079e5..3ac5ed73c 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -76,6 +76,19 @@ This project adheres to [Semantic Versioning](https://semver.org/).### Fixed+- **OpenAI's gpt-5.6 models run with their reasoning on the agent's default+ configuration** (#1473). `/v1/chat/completions` refuses function tools for+ `gpt-5.6-terra` and `gpt-5.6-luna` unless `reasoning_effort` is `none`,+ including when no effort is configured and the model applies its own+ default, so a plain `LANGCHAIN_PROVIDER=openai` setup failed on its first+ tool call with `Function tools with reasoning_effort are not supported`.+ The adapter now treats that refusal the way it treats a rejected+ `temperature` or `stream_options`: the request is retried on+ `/v1/responses`, where the effort travels as `reasoning.effort`, and the+ model is remembered for the rest of the process so later calls go there+ first. Nothing changes for a model whose chat endpoint accepts tools, and+ `LANGCHAIN_USE_RESPONSES_API=true` still selects the route up front and+ skips the one failed request.- **A redacted answer keeps its row numbers and its labels** (#1471). When thegrounding gate released a draft with its unverified figures cut, a rankedtable came back with `(omitted※)` in place of 1, 2, 3, of the `12m` in itsdiff --git a/agent/.env.example b/agent/.env.exampleindex 79ca7ed84..e5a46b741 100644--- a/agent/.env.example+++ b/agent/.env.example@@ -180,6 +180,9 @@ MAX_RETRIES=2# Set this to literal true to use /v1/responses and send reasoning.effort instead.# The configured endpoint must support /v1/responses; native adapters keep# their provider-specific transport.+# A model whose Chat Completions endpoint refuses function tools with a reasoning+# effort (OpenAI's gpt-5.6-*) is retried on /v1/responses and stays there for the+# rest of the process; setting this to true skips that one failed request.# LANGCHAIN_USE_RESPONSES_API=true# ============================================================================diff --git a/agent/src/providers/llm.py b/agent/src/providers/llm.pyindex 42695cd6c..30047e237 100644--- a/agent/src/providers/llm.py+++ b/agent/src/providers/llm.py@@ -571,7 +571,7 @@ if ChatOpenAI is not None:try:return super()._generate(*args, **kwargs)except Exception as exc: # noqa: BLE001 - retried or re-raised below- if not self._remember_temperature_unsupported(exc):+ if not self._recoverable_rejection(exc):raisereturn super()._generate(*args, **kwargs)@@ -579,7 +579,7 @@ if ChatOpenAI is not None:try:return await super()._agenerate(*args, **kwargs)except Exception as exc: # noqa: BLE001 - retried or re-raised below- if not self._remember_temperature_unsupported(exc):+ if not self._recoverable_rejection(exc):raisereturn await super()._agenerate(*args, **kwargs)@@ -605,6 +605,47 @@ if ChatOpenAI is not None:_ANTHROPIC_TEMPERATURE_UNSUPPORTED.add(model)return True+ def _use_responses_api(self, payload: dict) -> bool: # type: ignore[override]+ """Route a model whose chat endpoint refused tools to /v1/responses."""+ if str(self.model_name) in _RESPONSES_REQUIRED:+ return True+ return super()._use_responses_api(payload)++ def _remember_responses_required(self, exc: BaseException) -> bool:+ """Record a chat-completions refusal naming /v1/responses; report a one-shot retry.++ OpenAI's gpt-5.6-* reject function tools on ``/v1/chat/completions``+ unless ``reasoning_effort`` is 'none', which would run the model+ without its reasoning; the same request on ``/v1/responses`` is+ accepted with the effort under ``reasoning.effort`` (issue #1473).+ The model is remembered in ``_RESPONSES_REQUIRED`` so later requests+ take that route up front. A model that already failed there is not+ retried again.+ """+ if not _is_responses_required_error(exc):+ return False+ model = str(self.model_name)+ if model in _RESPONSES_REQUIRED or self.use_responses_api is True:+ return False+ logger.warning(+ "Model %s refuses function tools on /v1/chat/completions with a "+ "reasoning effort; retrying on /v1/responses and using it for "+ "subsequent calls. Set LANGCHAIN_USE_RESPONSES_API=true to select "+ "it up front.",+ model,+ )+ _RESPONSES_REQUIRED.add(model)+ return True++ def _recoverable_rejection(self, exc: BaseException) -> bool:+ """Report whether a failed request should be sent once more.++ Each branch records what the endpoint rejected so the retry, and+ every later request, is shaped differently: without ``temperature``+ (#1223) or on ``/v1/responses`` (#1473).+ """+ return self._remember_temperature_unsupported(exc) or self._remember_responses_required(exc)+def _stream(self, *args: Any, **kwargs: Any) -> Iterator[Any]:"""Route Responses streams through the mapping-compatible adapter."""if self._use_responses_api({**kwargs, **self.model_kwargs}):@@ -624,7 +665,7 @@ if ChatOpenAI is not None:emitted = Trueyield chunkexcept Exception as exc: # noqa: BLE001 - retried or re-raised below- if emitted or not self._remember_temperature_unsupported(exc):+ if emitted or not self._recoverable_rejection(exc):raiseyield from self._stream(*args, **kwargs)@@ -696,7 +737,7 @@ if ChatOpenAI is not None:emitted = Trueyield chunkexcept Exception as exc: # noqa: BLE001 - retried or re-raised below- if emitted or not self._remember_temperature_unsupported(exc):+ if emitted or not self._recoverable_rejection(exc):raiseasync for chunk in self._astream(*args, **kwargs):yield chunk@@ -1013,6 +1054,34 @@ def _is_stream_usage_unsupported_error(exc: BaseException) -> bool:or "not allowed" in message)+# Models whose Chat Completions endpoint refused function tools and named+# /v1/responses as the way out (issue #1473). OpenAI's gpt-5.6-* answer HTTP 400+# "Function tools with reasoning_effort are not supported for <model> in+# /v1/chat/completions. To use function tools, use /v1/responses or set+# reasoning_effort to 'none'" whenever the request carries tools and the effort+# is anything but 'none' -- the model's own default when none is configured+# included. Setting 'none' would run the model without its reasoning, so the+# request is retried on /v1/responses instead, where the effort travels as+# ``reasoning.effort``. Membership is populated on that first refusal and reused+# process-wide so later calls skip the failed request.+_RESPONSES_REQUIRED: set[str] = set()+++def _is_responses_required_error(exc: BaseException) -> bool:+ """Return True when an endpoint refused tools and pointed at /v1/responses.++ Matches the endpoint's own instruction, not a model name: the message has+ to name ``reasoning_effort``, say it is not supported, and offer+ ``/v1/responses``. A gateway that merely rejects ``reasoning_effort`` as an+ unknown field does not match, because nothing says its Responses route+ would fare better.+ """+ message = str(getattr(exc, "message", "") or exc).lower()+ if "reasoning_effort" not in message or "/v1/responses" not in message:+ return False+ return "not supported" in message or "unsupported" in message++# Cache of base ChatAnthropic class -> temperature-safe subclass, so the dynamic# subclass is built once per resolved base class (keyed to support test doubles)._TEMPERATURE_SAFE_ANTHROPIC_CACHE: dict[type, type] = {}diff --git a/agent/tests/test_llm_responses_self_heal.py b/agent/tests/test_llm_responses_self_heal.pynew file mode 100644index 000000000..ab58f2d8a--- /dev/null+++ b/agent/tests/test_llm_responses_self_heal.py@@ -0,0 +1,327 @@+"""A chat-completions refusal that names /v1/responses is healed there (#1473).++OpenAI's gpt-5.6-* reject function tools on ``/v1/chat/completions`` unless+``reasoning_effort`` is 'none' -- the model's own default effort included, so a+plain ``LANGCHAIN_PROVIDER=openai`` setup failed on its first tool call::++ Function tools with reasoning_effort are not supported for gpt-5.6-terra in+ /v1/chat/completions. To use function tools, use /v1/responses or set+ reasoning_effort to 'none'.++The adapter treats that refusal like a rejected ``temperature`` (#1223) or+``stream_options`` (#1224): retry once on ``/v1/responses`` and remember the+model. These tests replay the endpoint's message verbatim through a mock+transport; the live endpoint is the reporter's to confirm.+"""++from __future__ import annotations++import json+from typing import Any, Iterator++import httpx+import pytest++import src.providers.llm as llm_mod+from src.providers.llm import ChatOpenAIWithReasoning, _is_responses_required_error++pytestmark = pytest.mark.skipif(+ ChatOpenAIWithReasoning is None, reason="langchain-openai is not installed"+)++_REFUSAL = (+ "Function tools with reasoning_effort are not supported for {model} in "+ "/v1/chat/completions. To use function tools, use /v1/responses or set "+ "reasoning_effort to 'none'."+)++_TOOL = {+ "type": "function",+ "function": {+ "name": "get_price",+ "description": "Last price of a symbol",+ "parameters": {+ "type": "object",+ "properties": {"symbol": {"type": "string"}},+ "required": ["symbol"],+ },+ },+}++[email protected](autouse=True)+def _forget_healed_models() -> Iterator[None]:+ """The memory is process-wide; no test may inherit another's entry."""+ llm_mod._RESPONSES_REQUIRED.clear()+ yield+ llm_mod._RESPONSES_REQUIRED.clear()+++def _refusal(model: str) -> httpx.Response:+ return httpx.Response(+ 400,+ json={+ "error": {+ "message": _REFUSAL.format(model=model),+ "type": "invalid_request_error",+ "param": "reasoning_effort",+ "code": None,+ }+ },+ )+++def _responses_tool_call(model: str) -> httpx.Response:+ return httpx.Response(+ 200,+ json={+ "id": "resp_1",+ "object": "response",+ "created_at": 0,+ "model": model,+ "output": [+ {+ "id": "fc_1",+ "type": "function_call",+ "call_id": "call_1",+ "name": "get_price",+ "arguments": json.dumps({"symbol": "AAPL"}),+ "status": "completed",+ }+ ],+ "parallel_tool_calls": True,+ "tool_choice": "auto",+ "tools": [],+ "usage": {+ "input_tokens": 5,+ "output_tokens": 3,+ "total_tokens": 8,+ "input_tokens_details": {"cached_tokens": 0},+ "output_tokens_details": {"reasoning_tokens": 0},+ },+ },+ )+++def _responses_text_stream(model: str) -> httpx.Response:+ completed = {+ "id": "resp_2",+ "object": "response",+ "created_at": 0,+ "model": model,+ "output": [+ {+ "id": "msg_1",+ "type": "message",+ "role": "assistant",+ "status": "completed",+ "content": [{"type": "output_text", "text": "hello", "annotations": []}],+ }+ ],+ "parallel_tool_calls": True,+ "tool_choice": "auto",+ "tools": [],+ }+ events = [+ {+ "type": "response.output_text.delta",+ "sequence_number": 0,+ "item_id": "msg_1",+ "output_index": 0,+ "content_index": 0,+ "delta": "hel",+ },+ {+ "type": "response.output_text.delta",+ "sequence_number": 1,+ "item_id": "msg_1",+ "output_index": 0,+ "content_index": 0,+ "delta": "lo",+ },+ {"type": "response.completed", "sequence_number": 2, "response": completed},+ ]+ body = "".join(f"data: {json.dumps(event)}\n\n" for event in events)+ return httpx.Response(200, headers={"content-type": "text/event-stream"}, content=body)+++def _chat_ok(model: str) -> httpx.Response:+ return httpx.Response(+ 200,+ json={+ "id": "chatcmpl_1",+ "object": "chat.completion",+ "created": 0,+ "model": model,+ "choices": [+ {+ "index": 0,+ "message": {"role": "assistant", "content": "ok"},+ "finish_reason": "stop",+ }+ ],+ },+ )+++class _Endpoint:+ """Record every request; answer by path."""++ def __init__(self, model: str, *, chat: Any = None, responses: Any = None) -> None:+ self.model = model+ self.chat = chat or (lambda request: _refusal(model))+ self.responses = responses or (lambda request: _responses_tool_call(model))+ self.paths: list[str] = []+ self.bodies: list[dict[str, Any]] = []++ def __call__(self, request: httpx.Request) -> httpx.Response:+ self.paths.append(request.url.path)+ self.bodies.append(json.loads(request.content))+ if request.url.path == "/v1/chat/completions":+ return self.chat(request)+ if request.url.path == "/v1/responses":+ return self.responses(request)+ raise AssertionError(f"unexpected path {request.url.path}")+++def _adapter(client: httpx.Client, model: str, **overrides: Any) -> ChatOpenAIWithReasoning:+ kwargs: dict[str, Any] = {+ "model": model,+ "api_key": "sk-test",+ "base_url": "https://api.openai.invalid/v1",+ "http_client": client,+ "use_responses_api": False,+ "max_retries": 0,+ "vibe_provider": "openai",+ "vibe_api_key": "sk-test",+ }+ kwargs.update(overrides)+ return ChatOpenAIWithReasoning(**kwargs)+++def test_a_refusal_naming_responses_is_retried_there_with_the_tools() -> None:+ endpoint = _Endpoint("gpt-5.6-terra")+ with httpx.Client(transport=httpx.MockTransport(endpoint)) as client:+ llm = _adapter(client, "gpt-5.6-terra").bind_tools([_TOOL])+ message = llm.invoke("price of AAPL?")++ assert endpoint.paths == ["/v1/chat/completions", "/v1/responses"]+ assert [call["name"] for call in message.tool_calls] == ["get_price"]+ assert message.tool_calls[0]["args"] == {"symbol": "AAPL"}+ sent = endpoint.bodies[1]+ assert sent["tools"] == [{"type": "function", **_TOOL["function"]}]+ assert "messages" not in sent and sent["input"], "the Responses request uses `input`"+ assert "reasoning_effort" not in sent, "no effort was configured; none is invented"+ assert "gpt-5.6-terra" in llm_mod._RESPONSES_REQUIRED+++def test_the_model_is_remembered_so_the_next_call_skips_the_refusal() -> None:+ endpoint = _Endpoint("gpt-5.6-luna")+ with httpx.Client(transport=httpx.MockTransport(endpoint)) as client:+ llm = _adapter(client, "gpt-5.6-luna").bind_tools([_TOOL])+ llm.invoke("first")+ llm.invoke("second")+ again = _adapter(client, "gpt-5.6-luna").bind_tools([_TOOL])+ again.invoke("third, from a fresh adapter in the same process")++ assert endpoint.paths == [+ "/v1/chat/completions",+ "/v1/responses",+ "/v1/responses",+ "/v1/responses",+ ]+++def test_a_configured_effort_travels_as_reasoning_effort_on_the_retry() -> None:+ endpoint = _Endpoint("gpt-5.6-terra")+ with httpx.Client(transport=httpx.MockTransport(endpoint)) as client:+ llm = _adapter(client, "gpt-5.6-terra", reasoning_effort="high").bind_tools([_TOOL])+ llm.invoke("price?")++ assert endpoint.bodies[0]["reasoning_effort"] == "high"+ assert endpoint.bodies[1]["reasoning"] == {"effort": "high"}+ assert "reasoning_effort" not in endpoint.bodies[1]+++def test_the_stream_path_retries_before_any_chunk_was_emitted() -> None:+ endpoint = _Endpoint(+ "gpt-5.6-terra", responses=lambda request: _responses_text_stream("gpt-5.6-terra")+ )+ with httpx.Client(transport=httpx.MockTransport(endpoint)) as client:+ llm = _adapter(client, "gpt-5.6-terra").bind_tools([_TOOL])+ text = "".join(chunk.text for chunk in llm.stream("hi"))++ assert text == "hello"+ assert endpoint.paths == ["/v1/chat/completions", "/v1/responses"]+ assert endpoint.bodies[1]["stream"] is True+++def test_a_model_whose_chat_endpoint_accepts_tools_is_left_alone() -> None:+ endpoint = _Endpoint("gpt-4.1", chat=lambda request: _chat_ok("gpt-4.1"))+ with httpx.Client(transport=httpx.MockTransport(endpoint)) as client:+ llm = _adapter(client, "gpt-4.1", reasoning_effort="high").bind_tools([_TOOL])+ assert llm.invoke("hi").content == "ok"++ assert endpoint.paths == ["/v1/chat/completions"]+ assert llm_mod._RESPONSES_REQUIRED == set()+++def test_an_unrelated_400_is_raised_untouched_and_nothing_is_remembered() -> None:+ def unknown_field(request: httpx.Request) -> httpx.Response:+ return httpx.Response(+ 400,+ json={+ "error": {+ "message": "Unrecognized request argument supplied: reasoning_effort",+ "type": "invalid_request_error",+ "param": "reasoning_effort",+ }+ },+ )++ endpoint = _Endpoint("house-model", chat=unknown_field)+ with httpx.Client(transport=httpx.MockTransport(endpoint)) as client:+ llm = _adapter(client, "house-model", reasoning_effort="high").bind_tools([_TOOL])+ with pytest.raises(Exception, match="Unrecognized request argument"):+ llm.invoke("hi")++ assert endpoint.paths == ["/v1/chat/completions"], "a gateway's unknown-field error is not rerouted"+ assert llm_mod._RESPONSES_REQUIRED == set()+++def test_a_refusal_on_the_responses_route_itself_is_not_retried_forever() -> None:+ """If /v1/responses answers with the same text, the error surfaces once."""+ endpoint = _Endpoint("gpt-5.6-terra", responses=lambda request: _refusal("gpt-5.6-terra"))+ with httpx.Client(transport=httpx.MockTransport(endpoint)) as client:+ llm = _adapter(client, "gpt-5.6-terra").bind_tools([_TOOL])+ with pytest.raises(Exception, match="not supported"):+ llm.invoke("hi")++ assert endpoint.paths == ["/v1/chat/completions", "/v1/responses"]+++def test_an_explicit_responses_route_is_never_rerouted() -> None:+ """Explicit ``LANGCHAIN_USE_RESPONSES_API=true`` owns the transport."""+ endpoint = _Endpoint("gpt-5.6-terra", responses=lambda request: _refusal("gpt-5.6-terra"))+ with httpx.Client(transport=httpx.MockTransport(endpoint)) as client:+ llm = _adapter(client, "gpt-5.6-terra", use_responses_api=True, output_version="responses/v1")+ with pytest.raises(Exception, match="not supported"):+ llm.bind_tools([_TOOL]).invoke("hi")++ assert endpoint.paths == ["/v1/responses"]+ assert llm_mod._RESPONSES_REQUIRED == set()+++ ("message", "expected"),+ [+ (_REFUSAL.format(model="gpt-5.6-sol"), True),+ ("Error code: 400 - {'error': {'message': \"" + _REFUSAL.format(model="gpt-5.6-terra") + "\"}}", True),+ ("Unrecognized request argument supplied: reasoning_effort", False),+ ("stream_options is not supported by this endpoint", False),+ ("reasoning_effort: use /v1/responses", False),+ ("", False),+ ],+)+def test_the_matcher_reads_the_endpoints_instruction(message: str, expected: bool) -> None:+ assert _is_responses_required_error(RuntimeError(message)) is expected