fix(tests): lint api_type test, cover the api_base override route, document STRIX_API_TYPE

This commit is contained in:
Alex Schapiro 2026-09-16 14:50:44 +00:00
parent bb15b95cf8
commit 7968ab44c6
2 changed files with 53 additions and 8 deletions

View file

@ -19,6 +19,12 @@ Configure Strix using environment variables or a config file.
Custom API base URL. Also accepts `OPENAI_API_BASE`, `LITELLM_BASE_URL`, or `OLLAMA_API_BASE`.
</ParamField>
<ParamField path="STRIX_API_TYPE" type="string">
Select the OpenAI API path for the model: `responses` or `chat_completions`.
By default, a custom `LLM_API_BASE` uses chat completions. Set this variable
when your gateway requires the other API. Also accepts `STRIX_FORCE_API`.
</ParamField>
<ParamField path="LLM_EXTRA_HEADERS" type="string">
Extra HTTP headers sent on every LLM request, as a JSON object (e.g.
`{"X-Feature-Key":"value","X-Tenant":"acme"}`). Useful for OpenAI-compatible

View file

@ -2,20 +2,27 @@
from __future__ import annotations
import litellm
import pytest
from agents.extensions.models.litellm_model import LitellmModel
from agents.model_settings import ModelSettings
from agents.models import _openai_shared
from agents.models.openai_chatcompletions import OpenAIChatCompletionsModel
from agents.models.openai_responses import OpenAIResponsesModel
from strix.config.models import (
RECOMMENDED_MODEL_NAMES,
StrixProvider,
_NonStreamingModel,
_TurnGuardModel,
configure_sdk_model_defaults,
is_recommended_or_frontier_model,
request_timeout_extra_args,
routes_through_litellm,
supports_strict_tool_schemas,
uses_chat_completions_tool_schema,
)
from strix.config.settings import Settings
@pytest.mark.parametrize("model_name", RECOMMENDED_MODEL_NAMES)
@ -169,11 +176,43 @@ def test_routes_through_litellm_matches_the_provider(
model = model._inner
assert isinstance(model, LitellmModel) is litellm
def test_api_type_override_settings() -> None:
from strix.config.settings import LlmSettings, Settings
from strix.config.models import uses_chat_completions_tool_schema
settings_chat = Settings(llm=LlmSettings(api_type="chat_completions", model="gpt-4"))
assert uses_chat_completions_tool_schema("gpt-4", settings_chat) is True
settings_resp = Settings(llm=LlmSettings(api_type="responses", model="openai/gpt-4"))
assert uses_chat_completions_tool_schema("openai/gpt-4", settings_resp) is False
def test_api_type_override_settings(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setenv("STRIX_LLM", "gpt-4")
monkeypatch.setenv("STRIX_API_TYPE", "chat_completions")
assert uses_chat_completions_tool_schema("gpt-4", Settings()) is True
monkeypatch.setenv("STRIX_LLM", "openai/gpt-4")
monkeypatch.setenv("STRIX_API_TYPE", "responses")
assert uses_chat_completions_tool_schema("openai/gpt-4", Settings()) is False
@pytest.mark.parametrize(
("api_type", "expected"),
[
(None, OpenAIChatCompletionsModel),
("chat_completions", OpenAIChatCompletionsModel),
("responses", OpenAIResponsesModel),
],
)
def test_api_type_overrides_the_api_base_route(
monkeypatch: pytest.MonkeyPatch, api_type: str | None, expected: type
) -> None:
"""``LLM_API_BASE`` defaults to chat completions. ``STRIX_API_TYPE`` must win."""
monkeypatch.setattr(_openai_shared, "_use_responses_by_default", True)
monkeypatch.setattr(_openai_shared, "_default_openai_client", None)
monkeypatch.setattr(_openai_shared, "_default_openai_key", None)
monkeypatch.setattr(litellm, "api_key", None)
monkeypatch.setattr(litellm, "api_base", None)
monkeypatch.setenv("OPENAI_API_KEY", "test-key")
monkeypatch.setenv("OPENAI_BASE_URL", "")
monkeypatch.setenv("STRIX_LLM", "gpt-5")
monkeypatch.setenv("LLM_API_KEY", "test-key")
monkeypatch.setenv("LLM_API_BASE", "https://gateway.example/v1")
monkeypatch.delenv("STRIX_API_TYPE", raising=False)
if api_type is not None:
monkeypatch.setenv("STRIX_API_TYPE", api_type)
configure_sdk_model_defaults(Settings())
model = StrixProvider().get_model("gpt-5")
while isinstance(model, _NonStreamingModel | _TurnGuardModel):
model = model._inner
assert isinstance(model, expected)