Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(config): optional numeric env vars fall back to default instead of crashing config init

Lanre Shittu07761617e44f
diff --git a/agent/src/config/env_schema.py b/agent/src/config/env_schema.py
index fe8e0e7bb..8e0968bbb 100644
--- a/agent/src/config/env_schema.py
+++ b/agent/src/config/env_schema.py
@@ -20,9 +20,10 @@ Usage::
from __future__ import annotations
import os
+import types
from enum import Enum
-from typing import Annotated, Any
+from typing import Annotated, Any, Union, get_args, get_origin
from pydantic import BaseModel, BeforeValidator, ConfigDict, Field, model_validator
@@ -73,6 +74,22 @@ def _parse_responses_api_bool(v: Any) -> Any:
ResponsesApiBool = Annotated[bool, BeforeValidator(_parse_responses_api_bool)]
+def _numeric_type(annotation: Any) -> Any:
+ """Return ``int``/``float`` for that type or an ``Optional`` of it, else ``None``.
+
+ Handles both ``typing.Optional[int]`` and the ``int | None`` spelling,
+ which produce different ``get_origin`` results (``typing.Union`` vs.
+ ``types.UnionType``).
+ """
+ if annotation is int or annotation is float:
+ return annotation
+ if get_origin(annotation) in (Union, types.UnionType):
+ args = [a for a in get_args(annotation) if a is not type(None)]
+ if len(args) == 1 and args[0] in (int, float):
+ return args[0]
+ return None
+
+
# ---------------------------------------------------------------------------
# Base class
# ---------------------------------------------------------------------------
@@ -109,13 +126,13 @@ class _EnvBase(BaseModel):
env_val = os.environ[alias]
# Safe coercion for numeric fields: drop unparseable values
# so Pydantic falls back to the field default.
- annotation = field_info.annotation
- if annotation is int and isinstance(env_val, str):
+ numeric_type = _numeric_type(field_info.annotation)
+ if numeric_type is int and isinstance(env_val, str):
try:
env_val = int(env_val)
except ValueError:
continue
- elif annotation is float and isinstance(env_val, str):
+ elif numeric_type is float and isinstance(env_val, str):
try:
env_val = float(env_val)
except ValueError:
diff --git a/agent/tests/test_env_schema.py b/agent/tests/test_env_schema.py
index ac5bed882..696c35a70 100644
--- a/agent/tests/test_env_schema.py
+++ b/agent/tests/test_env_schema.py
@@ -222,6 +222,17 @@ class TestEnvConfigTypeCoercion:
c = EnvConfig()
assert c.agent_tuning.token_threshold == 40000
+ def test_invalid_optional_int_falls_back_to_default(self, monkeypatch: pytest.MonkeyPatch) -> None:
+ monkeypatch.setenv("ANTHROPIC_MAX_TOKENS", "not_a_number")
+ c = EnvConfig()
+ assert c.llm.anthropic_max_tokens is None
+
+ def test_valid_optional_int_is_coerced(self, monkeypatch: pytest.MonkeyPatch) -> None:
+ monkeypatch.setenv("ANTHROPIC_MAX_TOKENS", "4096")
+ c = EnvConfig()
+ assert c.llm.anthropic_max_tokens == 4096
+ assert isinstance(c.llm.anthropic_max_tokens, int)
+
def test_bool_coercion_from_env(self, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setenv("VIBE_TRADING_DATA_CACHE", "true")
c = EnvConfig()