airflow.providers.common.ai.utils.usage¶
Coerce a templated usage_limits dict into a real UsageLimits instance.
Functions¶
|
Coerce a rendered |
Module Contents¶
- airflow.providers.common.ai.utils.usage.coerce_usage_limits(usage_limits)[source]¶
Coerce a rendered
usage_limitsdict into a realUsageLimitsinstance.A
UsageLimitsinstance has neitherresolvenortemplate_fields, so Airflow’s template walk is a no-op on it even thoughusage_limitsis in the operators’template_fields. Passing a plain dict instead lets every field be templated, but the rendered value is then not in the Dag author’s control: a Variable that exists but is empty renders to"", and a typo renders to an arbitrary non-numeric string. This function performs the defensive, per-field parsing that keeps those failures loud and specific instead of aTypeErrorraised deep inside pydantic-ai.usage_limits is None: returned unchanged.usage_limitsis already aUsageLimitsinstance: returned unchanged, by identity – an author who built the object themselves owns its field values.usage_limitsis a dict: each value is coerced per itsUsageLimitsfield type (see_coerce_value) and the result is passed toUsageLimits(**...).
The container shape can’t be checked any earlier than this, at the operator’s
__init__:usage_limitsis a template field, so at__init__time it may still be a Jinja string that has not been rendered yet (the whole field written as a single expression), and this function is the first point that only ever sees the rendered value.- Raises:
TypeError – if
usage_limitsis not aUsageLimits, adict, orNone– a container-shape problem, checked first.ValueError – if the dict has an unknown key, or a value cannot be coerced to its field’s type, is not finite, or is negative – a value problem, checked per field.