"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(llm): keep top-level reasoning_effort a positive allowlist
Haozhe Wu3468b19c581a
diff --git a/agent/src/providers/capabilities.py b/agent/src/providers/capabilities.pyindex 9ff64ef2f..8f1b71f72 100644--- a/agent/src/providers/capabilities.py+++ b/agent/src/providers/capabilities.py@@ -28,6 +28,15 @@ class ProviderCapabilities:be normalized to ``""`` for strict providers.openrouter_reasoning_body: Whether ``extra_body.reasoning`` is a validOpenRouter request option.+ top_level_reasoning_effort: Whether the provider accepts a top-level+ ``reasoning_effort`` field on ``/v1/chat/completions``. **A positive+ allowlist, and it stays one.** Speaking the OpenAI wire format does+ not imply accepting every OpenAI field: an endpoint that validates+ its request body strictly rejects the unknown key and the call+ fails, so a provider gets this only once someone has watched a real+ request to it succeed. Leave it False until then — the cost of False+ is that ``LANGCHAIN_REASONING_EFFORT`` has no effect there, and the+ cost of a wrong True is that every request fails.default_headers: Provider-scoped headers passed to ChatOpenAI.native_adapter_package: Optional native adapter package to report."""@@ -40,6 +49,7 @@ class ProviderCapabilities:gemini_thought_signatures: bool = Falsenormalize_assistant_content: bool = Falseopenrouter_reasoning_body: bool = False+ top_level_reasoning_effort: bool = Falsedefault_headers: Mapping[str, str] = field(default_factory=dict)native_adapter_package: Optional[str] = None@@ -119,7 +129,17 @@ _OPENAI_CODEX_CAPABILITIES = ProviderCapabilities(_PROVIDERS: dict[str, ProviderCapabilities] = {- "openai": ProviderCapabilities("openai", "OPENAI_API_KEY", "OPENAI_BASE_URL"),+ # ``gpt-5.6-*`` reject function tools on /v1/chat/completions unless the+ # request carries an explicit ``reasoning_effort`` — including the literal+ # "none" — so this one is required, not merely accepted. Still gated on the+ # base URL at the call site: pointing OPENAI_BASE_URL at Ollama, LiteLLM or+ # a corporate proxy keeps the label while changing who answers.+ "openai": ProviderCapabilities(+ "openai",+ "OPENAI_API_KEY",+ "OPENAI_BASE_URL",+ top_level_reasoning_effort=True,+ ),"anthropic": ProviderCapabilities("anthropic","ANTHROPIC_API_KEY",@@ -143,11 +163,15 @@ _PROVIDERS: dict[str, ProviderCapabilities] = {capture_reasoning=True,openrouter_reasoning_body=True,),+ # Verified live against DeepSeek Flash in #1025: a chat-completions request+ # carrying top-level ``reasoning_effort`` came back with reasoning content+ # and non-zero reasoning usage."deepseek": ProviderCapabilities("deepseek","DEEPSEEK_API_KEY","DEEPSEEK_BASE_URL",capture_reasoning=True,+ top_level_reasoning_effort=True,native_adapter_package="langchain-deepseek",),"siliconflow-cn": ProviderCapabilities(diff --git a/agent/src/providers/llm.py b/agent/src/providers/llm.pyindex 1636fe1f8..b89b1e21c 100644--- a/agent/src/providers/llm.py+++ b/agent/src/providers/llm.py@@ -5,6 +5,7 @@ from __future__ import annotationsimport loggingimport osfrom copy import copy+from urllib.parse import urlparseimport refrom collections.abc import AsyncIterator, Iterator, Mapping, Sequencefrom importlib import import_module@@ -1038,7 +1039,61 @@ def _sync_provider_env() -> None:def _supports_top_level_reasoning_effort(caps: ProviderCapabilities) -> bool:- return caps.name not in {"anthropic", "openai-codex"} and not caps.openrouter_reasoning_body+ """Report whether a provider accepts a top-level ``reasoning_effort`` field.++ Reads the ``top_level_reasoning_effort`` capability flag, which is a+ positive allowlist: a provider is listed only once a real request to it has+ been observed to succeed. Speaking the OpenAI wire format does not imply+ accepting every OpenAI field, and an endpoint that validates its body+ strictly rejects the unknown key outright — so the default is off and the+ consequence of that default is a no-op, not a failed call.++ Relays that take the effort inside ``extra_body.reasoning`` (OpenRouter,+ Requesty) use that path instead and are excluded here.++ Args:+ caps: Canonical capabilities resolved for the provider and model. Note+ this is the *resolved* capability, so provider ``openai`` with a+ ``deepseek-*`` model arrives here as DeepSeek, and an unrecognised+ provider name arrives as OpenAI — which is why callers must also+ check the base URL before trusting the OpenAI entry.++ Returns:+ True when the provider is on the allowlist and does not use the+ ``extra_body.reasoning`` relay path.+ """+ return caps.top_level_reasoning_effort and not caps.openrouter_reasoning_body+++def _openai_label_points_at_openai(caps: ProviderCapabilities) -> bool:+ """Report whether the OpenAI capability is actually talking to OpenAI.++ An unknown ``LANGCHAIN_PROVIDER`` falls back to the OpenAI capabilities, and+ a base-URL override points the OpenAI client at some other gateway (Ollama,+ LiteLLM, a corporate proxy). Those speak the OpenAI wire format but need not+ accept ``reasoning_effort``, so the label alone is not enough to send it.++ Non-OpenAI capabilities are unaffected — they carry their own endpoint.+ """+ if caps.name != "openai":+ return True+ try:+ base_url = (+ get_llm_credentials("openai", get_env_config().llm.langchain_model_name)+ .get("base_url")+ or ""+ ).strip()+ except Exception: # noqa: BLE001 - a credential lookup must not break the check+ return False+ if not base_url:+ return True+ host = urlparse(base_url if "//" in base_url else f"https://{base_url}").hostname or ""+ return host.lower() in {"api.openai.com", "openai.com"}+++def _sends_top_level_reasoning_effort(caps: ProviderCapabilities) -> bool:+ """Combine the allowlist flag with the OpenAI base-URL check."""+ return _supports_top_level_reasoning_effort(caps) and _openai_label_points_at_openai(caps)def uses_responses_api(@@ -1176,7 +1231,7 @@ def provider_diagnostics() -> dict[str, Any]:"openrouter_reasoning_body": caps.openrouter_reasoning_body,"top_level_reasoning_effort": (adapter_type == "openai-compatible"- and _supports_top_level_reasoning_effort(caps)+ and _sends_top_level_reasoning_effort(caps)),},}@@ -1285,7 +1340,7 @@ def build_llm(*, model_name: Optional[str] = None, callbacks: Any = None) -> Anyif (effortand not use_responses_api- and _supports_top_level_reasoning_effort(caps)+ and _sends_top_level_reasoning_effort(caps))else None),diff --git a/agent/tests/test_llm_reasoning_effort.py b/agent/tests/test_llm_reasoning_effort.pyindex 7f75c6637..142b0cff6 100644--- a/agent/tests/test_llm_reasoning_effort.py+++ b/agent/tests/test_llm_reasoning_effort.py@@ -165,7 +165,14 @@ class TestDirectOpenAI:assert kwargs["reasoning_effort"] == "high"- def test_openai_compatible_endpoint_receives_effort(self) -> None:+ def test_foreign_gateway_behind_the_openai_label_is_not_sent_the_field(self) -> None:+ """A base-URL override means the OpenAI label is no longer OpenAI.++ Ollama, LiteLLM and corporate proxies speak the OpenAI wire format+ without promising to accept every OpenAI field, and a strict body+ validator rejects the unknown key outright. The label alone cannot+ authorize it — only the host can.+ """kwargs = _capture_kwargs({"LANGCHAIN_PROVIDER": "openai",@@ -176,6 +183,20 @@ class TestDirectOpenAI:})+ assert kwargs["reasoning_effort"] is None++ def test_openai_host_receives_the_field(self) -> None:+ """No override means the request really does reach api.openai.com."""+ kwargs = _capture_kwargs(+ {+ "LANGCHAIN_PROVIDER": "openai",+ "OPENAI_API_KEY": "sk-test",+ "LANGCHAIN_MODEL_NAME": "gpt-5.6-luna",+ "LANGCHAIN_REASONING_EFFORT": "high",+ "LANGCHAIN_USE_RESPONSES_API": "false",+ }+ )+assert kwargs["reasoning_effort"] == "high"def test_deepseek_flash_model_keeps_effort_on_openai_wire(self) -> None:@@ -222,7 +243,15 @@ class TestUnsupportedProviders:assert kwargs["reasoning_effort"] == "high"assert kwargs["extra_body"] is None- def test_gemini_openai_compatible_receives_top_level_effort(self) -> None:+ def test_gemini_is_not_on_the_allowlist(self) -> None:+ """Gemini's OpenAI-compatible endpoint has not been verified for this field.++ ``top_level_reasoning_effort`` is a positive allowlist: a provider joins+ it once a real request to it has been watched to succeed, not because it+ speaks the OpenAI wire format. Until then the effort is a no-op for+ Gemini, which is the safe failure — the alternative is every request+ failing on a rejected key.+ """kwargs = _capture_kwargs({"LANGCHAIN_PROVIDER": "gemini",@@ -234,9 +263,24 @@ class TestUnsupportedProviders:})- assert kwargs["reasoning_effort"] == "high"+ assert kwargs["reasoning_effort"] is Noneassert kwargs["extra_body"] is None+ def test_every_allowlisted_provider_has_a_recorded_reason(self) -> None:+ """The allowlist stays short and every entry is accounted for here.++ A new provider flipping ``top_level_reasoning_effort=True`` has to be+ added to this set deliberately, with live evidence, rather than picked+ up by a broad predicate. If this fails, someone widened the allowlist —+ confirm a real request to that endpoint succeeded before updating it.+ """+ from src.providers.capabilities import _PROVIDERS++ allowlisted = {+ name for name, caps in _PROVIDERS.items() if caps.top_level_reasoning_effort+ }+ assert allowlisted == {"openai", "deepseek"}+def test_explicit_openai_provider_keeps_effort_for_deepseek_model(self) -> None:kwargs = _capture_kwargs({