BerriAI/litellm · error · ValueError
limit must be non-negative
Error message
limit must be non-negative
What it means
After successfully converting limit to an int, get_usage_data rejects values below zero with ValueError('limit must be non-negative'). The value is then interpolated into the SQL LIMIT clause, so the guard prevents a malformed/negative LIMIT from reaching Postgres.
Solutions
- Use limit=None when you want no LIMIT clause (unlimited)
- Clamp user input: max(0, requested_limit)
- Validate pagination math (offset/size) before calling the export API
Example fix
# before df = await db.get_usage_data(limit=page_size - offset) # can go negative # after df = await db.get_usage_data(limit=max(0, page_size - offset) if page_size > offset else None)
Defensive patterns
Strategy: validation
Validate before calling
limit = None if user_limit is None else max(0, int(user_limit)) df = await db.get_usage_data(limit=limit)
Try / catch
try:
df = await db.get_usage_data(limit=limit)
except ValueError as e:
if 'limit must be non-negative' in str(e):
df = await db.get_usage_data(limit=abs(limit))
else:
raise Prevention
- Use None (not -1) as the 'unlimited' sentinel — the API maps None to no LIMIT clause
- Clamp pagination arithmetic results before passing them in
- Validate user-supplied limit ranges at the request boundary
When it happens
Trigger: get_usage_data(limit=-1); limit computed as a subtraction that underflows (e.g. page_size - offset); config defaulting to -1 as an 'unlimited' sentinel — this API uses None for unlimited, not -1.
Common situations: Porting code where -1 meant 'no limit'; user-supplied pagination input not clamped; arithmetic on defaults producing negatives.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- limit must be an integer
- BFL error
- bucket_name must be provided for GCS destination
- Cisco AI Defense guardrail: invalid rule definition
- Database not connected. Connect a database to your proxy -…
AI-assisted analysis of BerriAI/litellm@77b7c6c40c (2026-08-18).
Data as JSON: /api/errors/0ecf8aca0f88d258.
Report an issue: GitHub.
Appendix: source
Thrown at litellm/integrations/focus/database.py:57
query_params.append(start_time_utc)
placeholder_index += 1
if end_time_utc:
where_clauses.append(f"dus.updated_at <= ${placeholder_index}::timestamptz")
query_params.append(end_time_utc)
placeholder_index += 1
where_clause = ""
if where_clauses:
where_clause = "WHERE " + " AND ".join(where_clauses)
limit_clause = ""
if limit is not None:
try:
limit_value: Final = int(limit)
except (TypeError, ValueError) as exc: # pragma: no cover - defensive guard
raise ValueError("limit must be an integer") from exc
if limit_value < 0:
raise ValueError("limit must be non-negative")
limit_clause = f" LIMIT ${placeholder_index}"
query_params.append(limit_value)
query: Final = f"""
SELECT
dus.id,
dus.date,
dus.user_id,
dus.api_key,
dus.model,
dus.model_group,
dus.custom_llm_provider,
dus.prompt_tokens,
dus.completion_tokens,
dus.spend,
dus.api_requests,
dus.successful_requests,
dus.failed_requests,View on GitHub (pinned to 77b7c6c40c)