Repository navigation
Make OpenAI Responses token ceilings templated and surface truncation - #72150
Merged
Merged
Conversation
Lee-W
force-pushed
the
openai-response-token-ceilings
branch
from
September 7, 2026 08:34
c561530 to
0f11e54
Compare
Lee-W
marked this pull request as ready for review
September 7, 2026 08:35
Lee-W
force-pushed
the
openai-response-token-ceilings
branch
from
September 7, 2026 08:35
0f11e54 to
d589474
Compare
kaxil
approved these changes
Sep 8, 2026
amoghrajesh
reviewed
Sep 9, 2026
amoghrajesh
left a comment
Contributor
There was a problem hiding this comment.
Mostly agree with kaxil's comments.
Lee-W
force-pushed
the
openai-response-token-ceilings
branch
from
September 11, 2026 00:41
d589474 to
190ef7e
Compare
pankajastro
reviewed
Sep 11, 2026
Lee-W
force-pushed
the
openai-response-token-ceilings
branch
from
September 11, 2026 14:57
190ef7e to
0ea5e64
Compare
kaxil
reviewed
Sep 11, 2026
Lee-W
force-pushed
the
openai-response-token-ceilings
branch
from
September 13, 2026 00:08
0ea5e64 to
d633bb4
Compare
kaxil
approved these changes
Sep 13, 2026
kaxil
reviewed
Sep 14, 2026
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
force-pushed
the
openai-response-token-ceilings
branch
from
September 15, 2026 09:35
40c30f5 to
2334e14
Compare
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
force-pushed
the
openai-response-token-ceilings
branch
from
September 15, 2026 12:45
bfcfebe to
6a9a719
Compare
1 task done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Dags need a per-run token ceiling that can vary by environment, but
max_output_tokensandmax_tool_callswere only reachable through the non-templated response_kwargs dict.response_kwargsis rejected at Dag-parse time rather than on the first run.max_output_tokenscaps generated tokens. andmax_tool_callscaps built-in tool calls; both are enforced by the OpenAI API itself. Hittingmax_output_tokensdoes 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?
Generated-by: [Claude] following the guidelines
{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.