Skip to content

Commit d8712ce

Browse files
authored
fix(core): add error handling to _dump_failed_request to prevent crashes on read only filesystem (#6036)
1 parent 5a90a4b commit d8712ce

1 file changed

Lines changed: 32 additions & 28 deletions

File tree

core/framework/llm/litellm.py

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -342,34 +342,38 @@ def _dump_failed_request(
342342
attempt: int,
343343
) -> str:
344344
"""Dump failed request to a file for debugging. Returns the file path."""
345-
FAILED_REQUESTS_DIR.mkdir(parents=True, exist_ok=True)
346-
347-
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S_%f")
348-
filename = f"{error_type}_{model.replace('/', '_')}_{timestamp}.json"
349-
filepath = FAILED_REQUESTS_DIR / filename
350-
351-
# Build dump data
352-
messages = kwargs.get("messages", [])
353-
dump_data = {
354-
"timestamp": datetime.now().isoformat(),
355-
"model": model,
356-
"error_type": error_type,
357-
"attempt": attempt,
358-
"estimated_tokens": _estimate_tokens(model, messages),
359-
"num_messages": len(messages),
360-
"messages": messages,
361-
"tools": kwargs.get("tools"),
362-
"max_tokens": kwargs.get("max_tokens"),
363-
"temperature": kwargs.get("temperature"),
364-
}
365-
366-
with open(filepath, "w", encoding="utf-8") as f:
367-
json.dump(dump_data, f, indent=2, default=str)
368-
369-
# Prune old dumps to prevent unbounded disk growth
370-
_prune_failed_request_dumps()
371-
372-
return str(filepath)
345+
try:
346+
FAILED_REQUESTS_DIR.mkdir(parents=True, exist_ok=True)
347+
348+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S_%f")
349+
filename = f"{error_type}_{model.replace('/', '_')}_{timestamp}.json"
350+
filepath = FAILED_REQUESTS_DIR / filename
351+
352+
# Build dump data
353+
messages = kwargs.get("messages", [])
354+
dump_data = {
355+
"timestamp": datetime.now().isoformat(),
356+
"model": model,
357+
"error_type": error_type,
358+
"attempt": attempt,
359+
"estimated_tokens": _estimate_tokens(model, messages),
360+
"num_messages": len(messages),
361+
"messages": messages,
362+
"tools": kwargs.get("tools"),
363+
"max_tokens": kwargs.get("max_tokens"),
364+
"temperature": kwargs.get("temperature"),
365+
}
366+
367+
with open(filepath, "w", encoding="utf-8") as f:
368+
json.dump(dump_data, f, indent=2, default=str)
369+
370+
# Prune old dumps to prevent unbounded disk growth
371+
_prune_failed_request_dumps()
372+
373+
return str(filepath)
374+
except OSError as e:
375+
logger.warning(f"Failed to dump request debug log to {FAILED_REQUESTS_DIR}: {e}")
376+
return "log_write_failed"
373377

374378

375379
def _compute_retry_delay(

0 commit comments

Comments
 (0)