Skip to content

fix(core): pass __context/__tracer to async and streaming class-based actions - #937

Open
breken-ai wants to merge 1 commit into
apache:mainfrom
breken-ai:fix/class-action-injected-context
Open

breken-ai wants to merge 1 commit into
apache:mainfrom
breken-ai:fix/class-action-injected-context

Conversation

@breken-ai

Copy link
Copy Markdown

Class-based actions can take the injected __context (or __tracer) as a run parameter, but that only works on the sync, non-streaming path. An async def run(self, state, __context) action, or a StreamingAction / AsyncStreamingAction whose stream_run takes __context, fails when it runs:

TypeError: AsyncActionWithContext.run() got an unexpected keyword argument '__context'
TypeError: StreamingActionWithContext.stream_run() got an unexpected keyword argument '__context'

Inside a class body, Python name-mangles __context to _ClassName__context. #449 and #452 fixed this for _run_function by remapping the injected inputs to the mangled names with _remap_dunder_parameters. _arun_function, _run_multi_step_streaming_action and _arun_multi_step_streaming_action still pass __context= unchanged.

Changes

  • burr/core/application.py: add a small _remap_injected_inputs(method, inputs) helper that wraps the existing check-and-remap from _run_function. Call it in _run_function (behavior unchanged), _arun_function, and the sync and async multi-step streaming runners, on stream_run.
  • Function-based actions are unaffected. Their run takes **run_kwargs, so no parameter name ends in __context and the inputs pass through unchanged.
  • tests/core/test_application.py: three end-to-end regressions that build an app with a class-based action reading __context.app_id. They go through astep(), stream_result() and astream_result().

How I tested this

  • On main (8161159), the three new tests fail with the TypeErrors above.
  • With the fix, they pass. pytest tests/core: 403 passed (with the graphviz package installed; without it, only the existing graphviz display tests fail, the same as on main).
  • black 23.11.0 (--line-length=100), isort 5.12.0 and flake8 6.1.0 at the pre-commit pins are clean on the changed files. git diff --check is clean.

Notes

Checklist

  • PR has an informative and human-readable title (this will be pulled into the release notes)
  • Changes are limited to a single goal (no scope creep)
  • Code passed the pre-commit check & code is left cleaner/nicer than when first encountered.
  • Any change in functionality is tested
  • New functions are documented (with a description, list of inputs, and expected output) (_remap_injected_inputs is a private helper with a docstring)
  • Placeholder code is flagged / future TODOs are captured in comments (none added)
  • Project documentation has been updated if adding/changing functionality (no documented behavior changes; this makes the code match the existing __context docs)

AI disclosure (per the ASF Generative Tooling guidance): an AI coding tool (Claude Code, Claude Opus 5.5) wrote this change through the breken-ai account. The tool found the bug, wrote the fix and the tests, and wrote this description. The commit carries a Generated-by: trailer. The diff is a small original change to existing Burr code and includes no third-party material. The red/green runs above are real, and you can re-run them from the diff. If you would rather not take AI-assisted contributions here, say so and I will close this.

🤖 Generated with Claude Code

A class-based action that declares `__context` (or `__tracer`) in its
run signature has that parameter name-mangled by Python, e.g. to
`_MyAction__context`. The sync run path remaps the injected input to
the mangled name, but the async run path and both multi-step streaming
paths passed `__context=` as-is, so the action failed with
`TypeError: run() got an unexpected keyword argument '__context'`.

Apply the same remap in `_arun_function` and in the sync and async
multi-step streaming runners.

Generated-by: Claude Code (Claude Opus 5.5)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@github-actions github-actions Bot added the area/core Application, State, Graph, Actions label Sep 25, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/core Application, State, Graph, Actions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant