Skip to content

Commit af8d167

Browse files
committed
llm_services: fall back to ModelHTTPError when pydantic-ai has no ModelAPIError
requirements.txt pins pydantic-ai 1.0.6, which predates ModelAPIError, so the import failed and CI could not collect any test.
1 parent 1697ec8 commit af8d167

2 files changed

Lines changed: 12 additions & 6 deletions

File tree

‎codewiki/src/be/llm_services.py‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@
1313

1414
from openai.types import chat
1515

16-
from pydantic_ai.exceptions import ModelAPIError, ModelHTTPError, UnexpectedModelBehavior
16+
from pydantic_ai.exceptions import ModelHTTPError, UnexpectedModelBehavior
17+
18+
try: # pydantic-ai >= 1.x adds ModelAPIError as the parent of ModelHTTPError
19+
from pydantic_ai.exceptions import ModelAPIError
20+
except ImportError: # older releases (e.g. the pinned 1.0.6) only have ModelHTTPError
21+
ModelAPIError = ModelHTTPError
1722
from pydantic_ai.models.openai import OpenAIChatModel, OpenAIChatModelSettings
1823
from pydantic_ai.models.fallback import FallbackModel
1924
from pydantic_ai.providers.openai import OpenAIProvider
@@ -254,9 +259,10 @@ def create_fallback_models(config: Config) -> FallbackModel:
254259
"""Create fallback models chain from configuration."""
255260
main = create_main_model(config)
256261
fallback = create_fallback_model(config)
257-
# The default fallback_on=(ModelAPIError,) misses UnexpectedModelBehavior, which
258-
# pydantic-ai raises for a 200 response whose body does not parse (e.g. choices=None
259-
# from an OpenAI-compatible gateway); without it such a response skips the fallback.
262+
# The default fallback_on (ModelAPIError, or ModelHTTPError on older pydantic-ai)
263+
# misses UnexpectedModelBehavior, which pydantic-ai raises for a 200 response whose
264+
# body does not parse (e.g. choices=None from an OpenAI-compatible gateway); without
265+
# it such a response skips the fallback model.
260266
return FallbackModel(main, fallback, fallback_on=(ModelAPIError, UnexpectedModelBehavior))
261267

262268

‎tests/test_agent_limits.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from pathlib import Path
66
from types import SimpleNamespace
77

8-
from pydantic_ai.exceptions import ModelAPIError, UnexpectedModelBehavior
8+
from pydantic_ai.exceptions import UnexpectedModelBehavior
99

1010
import codewiki.src.be.agent_tools.generate_sub_module_documentations as sub_mod
1111
import codewiki.src.be.llm_services as llm_services
@@ -55,7 +55,7 @@ def test_fallback_covers_malformed_responses(monkeypatch):
5555
)
5656
llm_services.create_fallback_models(SimpleNamespace())
5757
# a 200 response with an unparseable body must also switch to the fallback model
58-
assert set(captured["fallback_on"]) == {ModelAPIError, UnexpectedModelBehavior}
58+
assert set(captured["fallback_on"]) == {llm_services.ModelAPIError, UnexpectedModelBehavior}
5959

6060

6161
def test_overview_system_prompt():

0 commit comments

Comments
 (0)