|
13 | 13 |
|
14 | 14 | from openai.types import chat |
15 | 15 |
|
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 |
17 | 22 | from pydantic_ai.models.openai import OpenAIChatModel, OpenAIChatModelSettings |
18 | 23 | from pydantic_ai.models.fallback import FallbackModel |
19 | 24 | from pydantic_ai.providers.openai import OpenAIProvider |
@@ -254,9 +259,10 @@ def create_fallback_models(config: Config) -> FallbackModel: |
254 | 259 | """Create fallback models chain from configuration.""" |
255 | 260 | main = create_main_model(config) |
256 | 261 | 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. |
260 | 266 | return FallbackModel(main, fallback, fallback_on=(ModelAPIError, UnexpectedModelBehavior)) |
261 | 267 |
|
262 | 268 |
|
|
0 commit comments