BerriAI/litellm · error · ValueError

limit must be an integer

Error message

limit must be an integer

What it means

get_usage_data() only appends ' LIMIT $3' to the SQL when limit is not None, and coerces it with int(limit) inside try/except (TypeError, ValueError). Non-coercible values (the string 'ten', lists, dicts) raise ValueError('limit must be an integer'). Numeric strings like '100' and floats (10.5, truncated) pass, so the error fires only for genuinely non-numeric input.

Solutions

  1. Pass a plain int or omit the limit parameter entirely
  2. Coerce once at your boundary: limit = int(limit) wrapped in try/except (TypeError, ValueError) with a clear error
  3. Declare limit as an int field in a pydantic model for whatever config/API feeds it

Example fix

# before
df = await db.get_usage_data(limit=os.environ['CZ_LIMIT'])  # '1k' -> ValueError: limit must be an integer

# after
limit = int(os.environ.get('CZ_LIMIT', '1000'))
df = await db.get_usage_data(limit=limit)
Defensive patterns

Strategy: validation

Validate before calling

def coerce_limit(value) -> int | None:
    if value is None:
        return None
    try:
        return int(value)
    except (TypeError, ValueError):
        raise ValueError(f'limit must be an integer, got {value!r}') from None

df = await db.get_usage_data(limit=coerce_limit(raw_limit))

Type guard

from typing import Any

def is_int_coercible(v: Any) -> bool:
    if v is None:
        return True
    if isinstance(v, bool):
        return False
    try:
        int(v)
        return True
    except (TypeError, ValueError):
        return False

Try / catch

except ValueError as e:
    if 'limit must be an integer' in str(e):
        return  # or return a 400 to your caller with the offending value

Prevention

When it happens

Trigger: await db.get_usage_data(limit='hundred'); limit=['100']; limit={'rows': 100}; an HTTP handler forwarding an unvalidated query-string param like ?limit=abc straight into the export call.

Common situations: Reading limit from an env var, CLI flag, or config YAML as a string like '1k'; API layers passing query params through without schema validation; 'None' vs None mixups in config parsing.

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


AI-assisted analysis of BerriAI/litellm@77b7c6c40c (2026-08-18). Data as JSON: /api/errors/8115e5927975e262. Report an issue: GitHub.

Appendix: source

Thrown at litellm/integrations/cloudzero/database.py:92

        FROM "LiteLLM_DailyUserSpend" dus
        LEFT JOIN "LiteLLM_VerificationToken" vt ON dus.api_key = vt.token
        LEFT JOIN "LiteLLM_TeamTable" tt ON vt.team_id = tt.team_id
        LEFT JOIN "LiteLLM_UserTable" ut ON dus.user_id = ut.user_id
        WHERE ($1::timestamptz IS NULL OR dus.updated_at >= $1::timestamptz)
          AND ($2::timestamptz IS NULL OR dus.updated_at <= $2::timestamptz)
        ORDER BY dus.date DESC, dus.created_at DESC
        """

        params: Final[list[Any]] = [
            start_time_utc,
            end_time_utc,
        ]

        if limit is not None:
            try:
                params.append(int(limit))
            except (TypeError, ValueError):
                raise ValueError("limit must be an integer")
            query += " LIMIT $3"

        try:
            db_response: Final = await client.db.query_raw(query, *params)
            # Convert the response to polars DataFrame with full schema inference
            # This prevents schema mismatch errors when data types vary across rows
            return pl.DataFrame(db_response, infer_schema_length=None)
        except Exception as e:
            raise Exception(f"Error retrieving usage data: {e}")

View on GitHub (pinned to 77b7c6c40c)