Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/fava_trails/llm/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@
# Enable unified exception hierarchy across all providers
os.environ.setdefault("ANY_LLM_UNIFIED_EXCEPTIONS", "1")

# Relax service_tier validation. OpenRouter may return values outside the
# OpenAI SDK Literal set, e.g. "standard", and any-llm revalidates the response
# against this model before returning it.
from any_llm.types.completion import ChatCompletion as _ChatCompletion

_service_tier = _ChatCompletion.model_fields.get("service_tier")
if _service_tier is not None:
_service_tier.annotation = str | None
_ChatCompletion.model_rebuild(force=True)

logger = logging.getLogger(__name__)


Expand Down
21 changes: 21 additions & 0 deletions tests/test_llm_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import pytest
from any_llm.exceptions import AuthenticationError, RateLimitError
from any_llm.types.completion import ChatCompletion

from fava_trails.llm.client import LLMClient, LLMError, LLMResponse

Expand Down Expand Up @@ -165,3 +166,23 @@ async def test_api_key_passed_to_acompletion(client):
call_kwargs = mock_acompletion.call_args.kwargs
assert call_kwargs["api_key"] == "or-key"
assert call_kwargs["provider"] == "openrouter"


def test_chatcompletion_accepts_nonstandard_service_tier():
"""Importing fava_trails.llm.client patches ChatCompletion to accept any service_tier string."""
data = {
"id": "gen-test",
"choices": [
{
"finish_reason": "stop",
"index": 0,
"message": {"role": "assistant", "content": "ok"},
}
],
"created": 1000000,
"model": "google/gemini-2.5-flash",
"object": "chat.completion",
"service_tier": "standard",
}
obj = ChatCompletion.model_validate(data)
assert obj.service_tier == "standard"
Loading