Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(providers): make the compat-path stream retry structural, and close the two #1223 side fixes

Haozhe Wu3e27d25ad20a
diff --git a/agent/.env.example b/agent/.env.example
index da99396df..c41cc3b75 100644
--- a/agent/.env.example
+++ b/agent/.env.example
@@ -24,6 +24,10 @@ OPENROUTER_BASE_URL=https://openrouter.ai/api/v1
# OPENAI_BASE_URL=https://api.openai.com/v1
# --- Anthropic Messages API / compatible proxy ---
+# Reach Claude models through LANGCHAIN_PROVIDER=anthropic (native) or through
+# ANTHROPIC_BASE_URL for a compatible proxy. Pointing OPENAI_BASE_URL at
+# Anthropic's OpenAI-compatibility endpoint is NOT a supported route: it takes
+# the generic OpenAI branch, which does not carry Anthropic's request shape.
# LANGCHAIN_PROVIDER=anthropic
# LANGCHAIN_MODEL_NAME=claude-sonnet-4-6
# ANTHROPIC_API_KEY=sk-ant-xxx
diff --git a/agent/src/config/env_schema.py b/agent/src/config/env_schema.py
index 3e9496524..be4ae07f4 100644
--- a/agent/src/config/env_schema.py
+++ b/agent/src/config/env_schema.py
@@ -139,6 +139,11 @@ class LLMConfig(_EnvBase):
langchain_provider: str = Field(alias="LANGCHAIN_PROVIDER", default="openai")
langchain_model_name: str = Field(alias="LANGCHAIN_MODEL_NAME", default="")
langchain_temperature: float = Field(alias="LANGCHAIN_TEMPERATURE", default=0.0)
+ # The native Anthropic credential. Read through bare ``os.getenv`` in
+ # ``llm.py``, which left it out of this schema — so ``provider doctor`` and
+ # preflight could not tell a missing key from a working one and the failure
+ # surfaced as an opaque 401 at the first call instead (#1223).
+ anthropic_api_key: str = Field(alias="ANTHROPIC_API_KEY", default="")
anthropic_max_tokens: int | None = Field(alias="ANTHROPIC_MAX_TOKENS", default=None, gt=0)
timeout_seconds: int = Field(alias="TIMEOUT_SECONDS", default=120)
max_retries: int = Field(alias="MAX_RETRIES", default=2)
diff --git a/agent/src/providers/llm.py b/agent/src/providers/llm.py
index 4d177953b..b2e9ddeb1 100644
--- a/agent/src/providers/llm.py
+++ b/agent/src/providers/llm.py
@@ -570,12 +570,18 @@ if ChatOpenAI is not None:
inner = super(ChatOpenAIWithReasoning, cloned)._stream(*args, **kwargs)
else:
inner = self._stream_with_usage_fallback(*args, **kwargs)
- # The temperature-unsupported error is raised before the first chunk is
- # produced, so retrying the whole stream cannot duplicate output.
+ # The 400 arrives before the first chunk, so restarting the stream
+ # cannot duplicate output — but that is a property of the provider,
+ # not of this code. Enforce it here instead of assuming it: once a
+ # chunk has been emitted the error is re-raised, because replaying
+ # the stream would emit those chunks a second time.
+ emitted = False
try:
- yield from inner
+ for chunk in inner:
+ emitted = True
+ yield chunk
except Exception as exc: # noqa: BLE001 - retried or re-raised below
- if not self._remember_temperature_unsupported(exc):
+ if emitted or not self._remember_temperature_unsupported(exc):
raise
yield from self._stream(*args, **kwargs)
@@ -636,13 +642,18 @@ if ChatOpenAI is not None:
inner = super(ChatOpenAIWithReasoning, cloned)._astream(*args, **kwargs)
else:
inner = self._astream_with_usage_fallback(*args, **kwargs)
- # The temperature-unsupported error is raised before the first chunk is
- # produced, so retrying the whole stream cannot duplicate output.
+ # The 400 arrives before the first chunk, so restarting the stream
+ # cannot duplicate output — but that is a property of the provider,
+ # not of this code. Enforce it here instead of assuming it: once a
+ # chunk has been emitted the error is re-raised, because replaying
+ # the stream would emit those chunks a second time.
+ emitted = False
try:
async for chunk in inner:
+ emitted = True
yield chunk
except Exception as exc: # noqa: BLE001 - retried or re-raised below
- if not self._remember_temperature_unsupported(exc):
+ if emitted or not self._remember_temperature_unsupported(exc):
raise
async for chunk in self._astream(*args, **kwargs):
yield chunk