Skip to content

Commit 0f0b34e

Browse files
Merge pull request #14974 from deepanshululla/feature/sqs_pushes_errors
Error logging in SQS
2 parents e61619c + 198777f commit 0f0b34e

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

litellm/integrations/sqs.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from __future__ import annotations
88

99
import asyncio
10+
import traceback
1011
from typing import List, Optional
1112

1213
import litellm
@@ -200,6 +201,25 @@ async def async_log_success_event(
200201
except Exception as e:
201202
verbose_logger.exception(f"sqs Layer Error - {str(e)}")
202203

204+
async def async_log_failure_event(self, kwargs, response_obj, start_time, end_time):
205+
try:
206+
standard_logging_payload = kwargs.get("standard_logging_object")
207+
if standard_logging_payload is None:
208+
raise ValueError("standard_logging_payload is None")
209+
210+
self.log_queue.append(standard_logging_payload)
211+
verbose_logger.debug(
212+
"sqs logging: queue length %s, batch size %s",
213+
len(self.log_queue),
214+
self.batch_size,
215+
)
216+
217+
except Exception as e:
218+
verbose_logger.exception(
219+
f"Datadog Layer Error - {str(e)}\n{traceback.format_exc()}"
220+
)
221+
pass
222+
203223
async def async_send_batch(self) -> None:
204224
verbose_logger.debug(
205225
f"sqs logger - sending batch of {len(self.log_queue)}"

tests/logging_callback_tests/test_sqs_logger.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,66 @@ async def test_async_sqs_logger_flush():
7171
assert len(payload_data["messages"]) == 1
7272
assert payload_data["messages"][0]["role"] == "user"
7373
assert payload_data["messages"][0]["content"] == "hello"
74+
75+
76+
@pytest.mark.asyncio
77+
async def test_async_sqs_logger_error_flush():
78+
expected_queue_url = "https://sqs.us-east-1.amazonaws.com/123456789012/test-queue"
79+
expected_region = "us-east-1"
80+
81+
sqs_logger = SQSLogger(
82+
sqs_queue_url=expected_queue_url,
83+
sqs_region_name=expected_region,
84+
sqs_flush_interval=1,
85+
)
86+
87+
# Mock the httpx client
88+
mock_response = MagicMock()
89+
mock_response.raise_for_status = Exception("Something went wrong")
90+
sqs_logger.async_httpx_client.post = AsyncMock(return_value=mock_response)
91+
92+
litellm.callbacks = [sqs_logger]
93+
94+
await litellm.acompletion(
95+
model="gpt-4o",
96+
messages=[{"role": "user", "content": "hello"}],
97+
mock_response="Error occurred"
98+
)
99+
100+
await asyncio.sleep(2)
101+
102+
# Verify that httpx post was called
103+
sqs_logger.async_httpx_client.post.assert_called()
104+
105+
# Get the call arguments
106+
call_args = sqs_logger.async_httpx_client.post.call_args
107+
108+
# Verify the URL is correct
109+
called_url = call_args[0][0] # First positional argument
110+
assert called_url == expected_queue_url, f"Expected URL {expected_queue_url}, got {called_url}"
111+
112+
# Verify the payload contains StandardLoggingPayload data
113+
called_data = call_args.kwargs['data']
114+
115+
# Extract the MessageBody from the URL-encoded data
116+
# Format: "Action=SendMessage&Version=2012-11-05&MessageBody=<url_encoded_json>"
117+
assert "Action=SendMessage" in called_data
118+
assert "Version=2012-11-05" in called_data
119+
assert "MessageBody=" in called_data
120+
121+
# Extract and decode the message body
122+
message_body_start = called_data.find("MessageBody=") + len("MessageBody=")
123+
message_body_encoded = called_data[message_body_start:]
124+
message_body_json = unquote(message_body_encoded)
125+
126+
# Parse the JSON to verify it's a StandardLoggingPayload
127+
payload_data = json.loads(message_body_json)
128+
129+
# Verify it has the expected StandardLoggingPayload structure
130+
assert "model" in payload_data
131+
assert "messages" in payload_data
132+
assert "response" in payload_data
133+
assert payload_data["model"] == "gpt-4o"
134+
assert len(payload_data["messages"]) == 1
135+
assert payload_data["messages"][0]["role"] == "user"
136+
assert payload_data["messages"][0]["content"] == "hello"

0 commit comments

Comments
 (0)