Files
crewAI/lib/crewai/tests/llms/openai/test_responses_only_models.py
alex-clawd 52ab798410 fix(openai): address CodeRabbit review on the reasoning_effort retry
Three defects, two raised in review and one found while verifying them.

1. Learned-behaviour caches ignored the endpoint (CodeRabbit).
   Both _LEARNED_TOOLS_REASONING_EFFORT_CONFLICTS and, from #6656,
   _LEARNED_RESPONSES_ONLY_MODELS were keyed on the model name alone. Two
   instances using the same model string against api.openai.com and an
   OpenAI-compatible proxy shared the cache, so one endpoint's 400 silently
   degraded the other. Both are now keyed by (endpoint, model) via
   _model_cache_key().

2. A recovered call still reported a failure (CodeRabbit).
   _handle_completion / _ahandle_completion log "OpenAI API call failed" and emit
   LLMCallFailedEvent for anything they catch, including the errors the retry
   wrappers exist to recover from. That printed a user-visible "LLM Error" panel
   for a call that then succeeded. Both handlers now re-raise early for errors
   _call_completions will retry, via _is_recoverable_completion_error() so the
   suppressing and retrying sides can't drift apart.

3. Dropping reasoning_effort didn't actually fix the request.
   Measured against the live endpoint while testing the above:

       gpt-5.6-sol  tools, no reasoning_effort   -> 400 (same error)
       gpt-5.6-sol  tools, reasoning_effort=none -> OK
       gpt-5.5      tools, no reasoning_effort   -> OK
       gpt-5.4      tools, no reasoning_effort   -> OK

   gpt-5.6-* apply a server-side default, so omitting the parameter fails exactly
   like sending "high". The retry and the fast path now send an explicit "none"
   instead of removing the key. Without this the whole retry was a no-op for the
   family that prompted the fix.

Also made error parsing tolerant of both body shapes the SDK produces: the live
BadRequestError carries "message"/"param" at the top level of .body, while a
synthetic one nests them under "error". The original code only read the nested
form, so detection returned False against real API errors and the retry never
fired. Shared _error_param_and_message() now handles both.

Verified live -- real Agent + Task + Crew().kickoff() on gpt-5.6-sol with the fast
path disabled so the runtime retry is exercised:

    AGENT RESULT: 391
    tool calls  : [(17, 23)]
    spurious LLMCallFailedEvent: 0
    spurious failure ERROR logs: 0

Tests: 153 passed, 1 skipped in tests/llms/openai. New cases for endpoint-scoped
learning on both caches, recoverable-error classification, and the forced-"none"
behaviour with the measured API results recorded. Ruff + mypy clean.
2026-07-26 02:21:58 -07:00

224 lines
8.0 KiB
Python

"""Tests for models that /v1/chat/completions doesn't serve at all.
The pro tier exists but is Responses-API-only. OpenAI reports it as a 404 that is
distinguishable from a genuine unknown model:
responses-only param="model", "only supported in v1/responses"
or "This is not a chat model"
genuine typo code="model_not_found", "does not exist"
Measured 2026-07 against the live endpoints: gpt-5-pro, gpt-5.2-pro, gpt-5.4-pro,
gpt-5.5-pro, o1-pro and o3-pro all 404 on chat completions and work on
/v1/responses. Rather than hardcoding that list, the 404 is caught and the call is
retried on the Responses API, then the model is remembered so the wasted round trip
is paid once per process.
"""
import httpx
import pytest
from openai import NotFoundError
from crewai.llms.providers.openai import completion as completion_module
from crewai.llms.providers.openai.completion import OpenAICompletion
MESSAGES = [{"role": "user", "content": "hi"}]
RESPONSES_ONLY_MESSAGES = (
"This model is only supported in v1/responses and not in /v1/chat/completions.",
"This is not a chat model and thus not supported in the v1/chat/completions "
"endpoint. Did you mean to use v1/completions?",
)
def build(model: str, **kwargs) -> OpenAICompletion:
return OpenAICompletion(model=model, api_key="sk-test", **kwargs)
def make_not_found(message: str, code: str | None = None) -> NotFoundError:
body = {
"error": {
"message": message,
"type": "invalid_request_error",
"param": None if code else "model",
"code": code,
}
}
response = httpx.Response(
status_code=404,
json=body,
request=httpx.Request("POST", "https://api.openai.com/v1/chat/completions"),
)
return NotFoundError(message, response=response, body=body)
@pytest.fixture(autouse=True)
def _clear_learned_models():
"""Keep the process-wide learned set from leaking between tests."""
completion_module._LEARNED_RESPONSES_ONLY_MODELS.clear()
yield
completion_module._LEARNED_RESPONSES_ONLY_MODELS.clear()
class TestErrorClassification:
@pytest.mark.parametrize("message", RESPONSES_ONLY_MESSAGES)
def test_detects_responses_only_404(self, message: str):
assert OpenAICompletion._is_responses_only_error(make_not_found(message))
def test_ignores_genuine_unknown_model(self):
"""A real typo must keep failing, not get retried on another endpoint."""
error = make_not_found(
"The model `gpt-5.99-fake` does not exist or you do not have access "
"to it.",
code="model_not_found",
)
assert not OpenAICompletion._is_responses_only_error(error)
def test_ignores_unrelated_exceptions(self):
assert not OpenAICompletion._is_responses_only_error(RuntimeError("boom"))
class TestFallback:
def test_retries_on_responses_and_remembers_the_model(self, monkeypatch):
llm = build("gpt-5-pro")
calls: list[str] = []
def fail_completion(**kwargs):
calls.append("completions")
raise ValueError("wrapped") from make_not_found(
RESPONSES_ONLY_MESSAGES[0]
)
monkeypatch.setattr(llm, "_handle_completion", fail_completion)
monkeypatch.setattr(
llm, "_call_responses", lambda **kwargs: calls.append("responses") or "ok"
)
assert llm._call_completions(MESSAGES) == "ok"
assert calls == ["completions", "responses"]
# Second call must skip the doomed chat-completions attempt.
assert llm._effective_api() == "responses"
def test_does_not_retry_genuine_unknown_model(self, monkeypatch):
llm = build("gpt-5.99-fake")
calls: list[str] = []
def fail_completion(**kwargs):
calls.append("completions")
raise ValueError("wrapped") from make_not_found(
"The model does not exist.", code="model_not_found"
)
monkeypatch.setattr(llm, "_handle_completion", fail_completion)
monkeypatch.setattr(
llm, "_call_responses", lambda **kwargs: calls.append("responses") or "ok"
)
with pytest.raises(ValueError):
llm._call_completions(MESSAGES)
assert calls == ["completions"]
assert not completion_module._LEARNED_RESPONSES_ONLY_MODELS
def test_custom_endpoint_never_falls_back(self, monkeypatch):
"""An OpenAI-compatible server may not implement /v1/responses at all.
Most (vLLM, LiteLLM proxies, Ollama) don't, so a self-hosted model must
keep the endpoint the user chose rather than being silently rerouted.
"""
llm = build(
"o1-pro", custom_openai=True, base_url="https://my-vllm.internal/v1"
)
calls: list[str] = []
def fail_completion(**kwargs):
calls.append("completions")
raise ValueError("wrapped") from make_not_found(
RESPONSES_ONLY_MESSAGES[0]
)
monkeypatch.setattr(llm, "_handle_completion", fail_completion)
monkeypatch.setattr(
llm, "_call_responses", lambda **kwargs: calls.append("responses") or "ok"
)
with pytest.raises(ValueError):
llm._call_completions(MESSAGES)
assert calls == ["completions"]
@pytest.mark.asyncio
async def test_async_path_falls_back_too(self, monkeypatch):
llm = build("gpt-5-pro")
calls: list[str] = []
async def fail_completion(**kwargs):
calls.append("completions")
raise ValueError("wrapped") from make_not_found(
RESPONSES_ONLY_MESSAGES[0]
)
async def ok_responses(**kwargs):
calls.append("responses")
return "ok"
monkeypatch.setattr(llm, "_ahandle_completion", fail_completion)
monkeypatch.setattr(llm, "_acall_responses", ok_responses)
assert await llm._acall_completions(MESSAGES) == "ok"
assert calls == ["completions", "responses"]
class TestEffectiveApi:
def test_defaults_to_completions_for_unknown_models(self):
"""Nothing is assumed up front; the 404 is what teaches us."""
assert build("gpt-5-pro")._effective_api() == "completions"
def test_explicit_responses_is_honoured(self):
assert build("gpt-5.5", api="responses")._effective_api() == "responses"
def test_learned_model_routes_directly(self):
llm = build("gpt-5-pro")
llm._remember_responses_only_model()
assert llm._effective_api() == "responses"
def test_learned_model_still_respects_custom_endpoint(self):
llm = build(
"gpt-5-pro", custom_openai=True, base_url="https://my-vllm.internal/v1"
)
completion_module._LEARNED_RESPONSES_ONLY_MODELS.add(llm._model_cache_key())
assert llm._effective_api() == "completions"
def test_learning_is_scoped_to_the_endpoint(self):
"""A conflict learned against one endpoint must not leak to another.
The same model name can be served by api.openai.com and by an
OpenAI-compatible proxy with different capabilities.
"""
real = build("gpt-5-pro")
proxy = build("gpt-5-pro", base_url="https://proxy.internal/v1")
real._remember_responses_only_model()
assert real._effective_api() == "responses"
assert proxy._effective_api() == "completions"
class TestNotFoundMessage:
@pytest.mark.parametrize("message", RESPONSES_ONLY_MESSAGES)
def test_points_at_responses_api(self, message: str):
msg = build("gpt-5.5")._model_not_found_message(make_not_found(message))
assert 'api="responses"' in msg
assert "not available on /v1/chat/completions" in msg
def test_keeps_plain_not_found_for_real_typos(self):
msg = build("gpt-5.5")._model_not_found_message(
make_not_found("The model does not exist.", code="model_not_found")
)
assert "not found" in msg
assert 'api="responses"' not in msg