Skip to main content

venice_ai.utils.parsing

Shared parsing helpers for safe type conversions.

These functions consolidate the duplicated _parse_int / _parse_float patterns that previously existed in:

  • venice_ai.exceptions (as _safe_int_parse / _safe_float_parse)
  • venice_ai.core.models.base.VeniceBaseModel (as instance methods)

All call-sites now delegate to these canonical implementations.

safe_int

def safe_int(value: str | None) -> int | None

Safely parse a string to int, returning None on failure.

Arguments:

  • value - The string to convert, or None.

Returns:

The parsed integer, or None if value is None or cannot be converted.

safe_float

def safe_float(value: str | None) -> float | None

Safely parse a string to float, returning None on failure.

Arguments:

  • value - The string to convert, or None.

Returns:

The parsed float, or None if value is None or cannot be converted.

ms_epoch_to_seconds

def ms_epoch_to_seconds(value: float | None) -> float | None

Normalize a possibly-millisecond-epoch numeric to seconds.

Several Venice rate-limit reset headers (e.g. x-ratelimit-reset-requests / x-ratelimit-reset-tokens) arrive as 13-digit absolute Unix epoch milliseconds (e.g. 1780580108941). Values whose magnitude is at or above 1e12 are treated as milliseconds and divided by 1000; smaller values (already in seconds, e.g. a 10-digit epoch) pass through untouched.

This mirrors :meth:venice_ai.core.models.base.VeniceBaseModel._ms_to_seconds so ms/seconds handling is symmetric across every reset-header consumer.

Arguments:

  • value - The numeric value to normalize, or None.

Returns:

The seconds-epoch value, or None if value is None.