Skip to content

Commit ad16c7f

Browse files
test(litellm): Replace mocks with httpx types in rate-limit test
1 parent c5b92c8 commit ad16c7f

2 files changed

Lines changed: 42 additions & 13 deletions

File tree

tests/conftest.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,6 +1080,28 @@ def inner(response_content, serialize_pydantic=False, request_headers=None):
10801080
return inner
10811081

10821082

1083+
@pytest.fixture
1084+
def get_rate_limit_model_response():
1085+
def inner(request_headers=None):
1086+
if request_headers is None:
1087+
request_headers = {}
1088+
1089+
model_request = HttpxRequest(
1090+
"POST",
1091+
"/responses",
1092+
headers=request_headers,
1093+
)
1094+
1095+
response = HttpxResponse(
1096+
429,
1097+
request=model_request,
1098+
)
1099+
1100+
return response
1101+
1102+
return inner
1103+
1104+
10831105
@pytest.fixture
10841106
def streaming_chat_completions_model_response():
10851107
return [

tests/integrations/litellm/test_litellm.py

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,9 @@ def test_embeddings_no_pii(
463463
assert SPANDATA.GEN_AI_EMBEDDINGS_INPUT not in span["data"]
464464

465465

466-
def test_exception_handling(sentry_init, capture_events):
466+
def test_exception_handling(
467+
reset_litellm_executor, sentry_init, capture_events, get_rate_limit_model_response
468+
):
467469
sentry_init(
468470
integrations=[LiteLLMIntegration()],
469471
traces_sample_rate=1.0,
@@ -472,19 +474,24 @@ def test_exception_handling(sentry_init, capture_events):
472474

473475
messages = [{"role": "user", "content": "Hello!"}]
474476

475-
with start_transaction(name="litellm test"):
476-
kwargs = {
477-
"model": "gpt-3.5-turbo",
478-
"messages": messages,
479-
}
477+
client = OpenAI(api_key="z")
480478

481-
_input_callback(kwargs)
482-
_failure_callback(
483-
kwargs,
484-
Exception("API rate limit reached"),
485-
datetime.now(),
486-
datetime.now(),
487-
)
479+
model_response = get_rate_limit_model_response()
480+
481+
with mock.patch.object(
482+
client.embeddings._client._client,
483+
"send",
484+
return_value=model_response,
485+
):
486+
with start_transaction(name="litellm test"):
487+
with pytest.raises(litellm.RateLimitError):
488+
litellm.completion(
489+
model="gpt-3.5-turbo",
490+
messages=messages,
491+
client=client,
492+
)
493+
494+
litellm_utils.executor.shutdown(wait=True)
488495

489496
# Should have error event and transaction
490497
assert len(events) >= 1

0 commit comments

Comments
 (0)