Skip to content

Make OpenAI Responses token ceilings templated and surface truncation - #72150

Merged
Lee-W merged 15 commits into
apache:mainfrom
astronomer:openai-response-token-ceilings
Sep 15, 2026
Merged

Lee-W merged 15 commits into
apache:mainfrom
astronomer:openai-response-token-ceilings

Conversation

@Lee-W

@Lee-W Lee-W commented Aug 27, 2026 •

Copy link
Copy Markdown
Member

Dags need a per-run token ceiling that can vary by environment, but max_output_tokens and max_tool_calls were only reachable through the non-templated response_kwargs dict.

  • Promote both to first-class, templated operator arguments, coerce the rendered string to a positive int, and reject an invalid or non-positive value.
  • A blank or whitespace-only rendered value is treated as unset - the literal "None" still raises.
  • Setting the same key both as an operator argument and inside response_kwargs is rejected at Dag-parse time rather than on the first run.

max_output_tokens caps generated tokens. and max_tool_calls caps built-in tool calls; both are enforced by the OpenAI API itself. Hitting max_output_tokens does not fail the request: the response comes back incomplete with truncated output text, which the previous 'may be empty' warning did not describe. The incomplete warning now reports incomplete_details.reason and only claims truncated-not-empty output for max_output_tokens, since a content filter can fire before any text exists.


Was generative AI tooling used to co-author this PR?
  • Yes (please specify the tool below)

Generated-by: [Claude] following the guidelines


  • Read the Pull Request Guidelines for more information. Note: commit author/co-author name and email in commits become permanently public when merged.
  • For fundamental code changes, an Airflow Improvement Proposal (AIP) is needed.
  • When adding dependency, check compliance with the ASF 3rd Party License Policy.
  • For significant user-facing changes create newsfragment: {pr_number}.significant.rst, in airflow-core/newsfragments. You can add this file in a follow-up commit after the PR is created so you know the PR number.

@Lee-W
Lee-W force-pushed the openai-response-token-ceilings branch from c561530 to 0f11e54 Compare September 7, 2026 08:34
@Lee-W
Lee-W marked this pull request as ready for review September 7, 2026 08:35
@Lee-W
Lee-W force-pushed the openai-response-token-ceilings branch from 0f11e54 to d589474 Compare September 7, 2026 08:35

@amoghrajesh amoghrajesh left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly agree with kaxil's comments.

@Lee-W
Lee-W force-pushed the openai-response-token-ceilings branch from d589474 to 190ef7e Compare September 11, 2026 00:41
@Lee-W
Lee-W force-pushed the openai-response-token-ceilings branch from 190ef7e to 0ea5e64 Compare September 11, 2026 14:57
@Lee-W
Lee-W force-pushed the openai-response-token-ceilings branch from 0ea5e64 to d633bb4 Compare September 13, 2026 00:08
Dags need a per-run token ceiling that can vary by environment, but
max_output_tokens and max_tool_calls were only reachable through the
non-templated response_kwargs dict. Promote both to first-class,
templated operator arguments, coerce the rendered string to a
positive int before the request goes out, and reject an invalid or
non-positive value instead of silently running with no ceiling.
Also reject the same key being set both as an argument and inside
response_kwargs, so one silently wins.

OpenAI's Responses API has no monetary cost limit to expose, so this
is a token ceiling only -- for a cost cap, use
apache-airflow-providers-common-ai instead. Hitting
max_output_tokens does not fail the request; the response comes
back incomplete with truncated output text, which the previous
'may be empty' warning did not describe. Surface
incomplete_details.reason in the log message so operators can tell
a truncation apart from other non-completed statuses.
max_tool_calls bounds how many built-in tool calls the model may make,
not how many tokens it generates, so calling both ceilings token-level
misleads readers about what the limit actually counts. The same sentence
appears in the operator docstring, which autoapi publishes as API docs.
Only max_output_tokens guarantees that the model produced partial text
before the ceiling stopped it. A content filter can fire before any
output exists, and an incomplete response carrying no reason tells us
nothing either way, so claiming truncated-not-empty in those cases sends
operators looking for output that was never returned.
Templating these ceilings took away the ability to express "no ceiling
this run": under default rendering there is no way to reach the None
path, so a Dag that varies a ceiling by environment had no value it
could render for the environments that want no limit.

The mutual-exclusion check between the operator argument and
response_kwargs needs neither rendering nor a run to decide, so it
belongs at task definition time where a misconfigured Dag surfaces as an
import error instead of a first-run failure.
…ents

The parameter-name/value tuple used to validate and to merge
max_output_tokens and max_tool_calls was written out twice, so adding a
third ceiling parameter later could update one loop and silently leave
the other behind. Pull it into one class-level constant both loops
share.

Two comments also restated the "blank means unset" rationale already
spelled out in the docstring and the operator docs, and a code-review
shorthand ("C2"/"C3") had leaked into a test comment that no reader
outside that review thread could resolve. Trim both to plain prose.
response_kwargs conflicts were already rejected in __init__, but the
type/positive-int check for max_output_tokens and max_tool_calls only
ran inside execute(), so a plain literal like max_output_tokens=0 still
passed Dag parsing and only failed once the task ran -- the same class
of late failure the parse-time conflict check was meant to avoid.

A string value may still be an unrendered template, so only non-string
literals (which can never come from Jinja rendering) are validated
eagerly; templated and plain string values keep waiting for execute().
…numeric types

The eager literal-value check added for max_output_tokens/max_tool_calls
treated anything that wasn't a str as an already-final value, so an
XComArg (another task's .output, a supported templated value that only
resolves at render time) was rejected at Dag-parse time even though it
was never actually invalid.

Switched both the eager check and the coercion helper from a denylist
to an allowlist: only bool/float/int are eager-validated at
construction, and the wire-bound coercion now rejects anything that
isn't int-or-str outright instead of handing it to int(), which
silently truncated Decimal/Fraction values (and would do the same for
other __int__-implementing numeric types) instead of rejecting them.
max_output_tokens documents itself as bounding reasoning tokens as well
as visible ones, so a reasoning model can spend the whole ceiling on
reasoning and return no message item at all -- the previous "truncated,
not empty" claim assumed reason=="max_output_tokens" always meant
partial text existed, which this case contradicts. Branching on
response.output_text, already in hand, is honest in every case; the
docstring and operator docs no longer promise truncated content that
may not exist.
{{ params.tokens or '' }} only renders to '' when tokens is a declared
param defaulting to None -- with StrictUndefined (the Dag default), an
undeclared tokens key raises UndefinedError instead of falling through.
{{ params.tokens | default('', true) }} renders to '' in both cases, so
switch the docstring, operator docs and system-test example to it.

Also note that hitting max_tool_calls, unlike max_output_tokens, leaves
no incomplete-status signal at all: the OpenAI SDK documents it as a
silent drop, with no status change and no incomplete_details, so a run
truncated this way is indistinguishable from a clean one in the logs
and in return_value.
_coerce_token_ceiling's parameter type only listed int | str, but the
eager literal check added in the previous commit passes it a value
already narrowed to bool | float | int -- the function's own body
already handles float explicitly (rejecting it, same as Decimal or
Fraction), so widen the signature to match what it actually accepts.
A ceiling that was asked for but resolved to None was indistinguishable
from one that was never set, so the request went out with no cap and
nothing recorded that the ceiling had been dropped. Two ordinary Dag
shapes reach that state: a ceiling bound to an upstream task's output
where nothing was pushed, and a Jinja template rendered under
render_template_as_native_obj. Without native rendering the same Dag
fails loudly, so enabling that flag silently turned a hard failure into
an uncapped run.

Blank and whitespace-only rendered values keep their "no ceiling for
this run" meaning; only a ceiling that was supplied and then resolved to
None now raises. Mapped tasks are out of scope: unmap() re-runs __init__
with already-resolved values, so an expanded None is indistinguishable
from an unset argument there.
The docs claimed literal ceilings are validated at Dag-parse time, which
overstates the check in two ways. Only bool, float and int are validated
eagerly, so a Decimal or Fraction constructs cleanly and is rejected only
once the task runs; and a mapped task's unmap() runs __init__ on the
worker rather than during parsing. A companion clause claimed those types
never arrive via Jinja rendering, which contradicted the neighbouring
note that native rendering delivers exactly such objects -- and read as
an argument for deleting the guard that keeps a truncated Decimal off the
wire.
The docs, the operator docstring and the example Dag all teach
`{{ params.tokens | default('', true) }}` for "no ceiling this run", but
nothing held that spelling in place. A test of this shape is easy to get
wrong: an operator with no Dag renders through a permissive environment,
where the rejected `or ''` spelling also yields an empty string, so the
case would pass either way and prove nothing. Both new cases bind a Dag
and the passing one asserts the environment it rendered through, so
losing that binding fails the assertion rather than going quiet.
@Lee-W
Lee-W force-pushed the openai-response-token-ceilings branch from 40c30f5 to 2334e14 Compare September 15, 2026 09:35
The Responses-API options list documents max_output_tokens and
max_tool_calls as response_kwargs keys, which was the only route when it
was written. They are operator arguments now, and the guide section above
it steers readers away from the dict, so the page recommended two
different things. Naming the arguments keeps one recommendation and gives
the reason a reader can act on: the argument is templated, the dict key
is not. The pass-through example in the docstring moves to a key that has
no dedicated argument.
Airflow 2's XComArg.resolve() reads context["expanded_ti_count"] unconditionally, so the fake render context raised KeyError there before the test could exercise the resolves-to-None path it is about.
@Lee-W
Lee-W force-pushed the openai-response-token-ceilings branch from bfcfebe to 6a9a719 Compare September 15, 2026 12:45
@Lee-W
Lee-W merged commit f5172d8 into apache:main Sep 15, 2026
79 checks passed
@Lee-W
Lee-W deleted the openai-response-token-ceilings branch September 15, 2026 15:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants