Skip to content

test(py): lock subflow context inheritance and the Dev UI context path - #6584

Merged
huangjeff5 merged 6 commits into
mainfrom
test/py-action-context-propagation
Oct 7, 2026
Merged

huangjeff5 merged 6 commits into
mainfrom
test/py-action-context-propagation

Conversation

@huangjeff5

@huangjeff5 huangjeff5 commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

A subflow sees the parent flow's context unless the call passes context=. This locks that contract with real @ai.flow tests, plus the Dev UI path that feeds context in. One runtime change: v1 runAction rejects a non-object context with 400, same as v2.

@ai.flow()
async def order_dish(dish: str) -> dict:
    inherited = await allergy_check(dish)
    replaced = (await allergy_check.run(dish, context={})).response
    return {'inherited': inherited, 'replaced': replaced}

await order_dish.run('Tartine', context={'auth': {'uid': 'u_42'}})
# => {'inherited': {'auth': {'uid': 'u_42'}}, 'replaced': {}}

Decisions

  • Inherit unless overridden, override replaces: Action.run sets the context ContextVar only when context is not None, so context={} is an override, not "no context". This matches JS runWithContext, which checks context === undefined. Tests cover replace, {}, and the parent getting its own context back after the subflow returns or raises.
  • Inheritance through Genkit code: .stream() runs the subflow as a task Genkit creates, and flow → generate → tool → subflow runs through the generate loop. Tests lock both. Plain gather / create_task are stdlib ContextVar copies and are not retested here.
  • Dev UI context reaches nested subflows: Tests run real flows through reflection v1 and v2 and check the flow and its subflow both see context. A v2 agent (bidi) run sees context too. A subflow's span records the inherited context with auth as <redacted>.
  • Non-object context is rejected on both servers: Previously v1 passed "context": "gold" straight to the flow, so ctx.context was a string and ctx.context.get(...) failed inside user code. Now v1 answers 400 before the flow runs, matching v2's invalid params. We chose rejecting over coercing to {} because a malformed request should fail where it is malformed.
  • Mutation is not locked: Context is passed by reference, so a subflow writing into ctx.context changes the parent's dict too, same as JS. These tests don't assert either way, so we can change that later without breaking them.

Verified: removing ContextVar inheritance fails 5 of these tests; removing the reset fails both restore tests; dropping context= from run_bidi_action fails the agent test; removing the v1 guard fails the v1 rejection test.

- A subflow sees the parent's context unless the call passes context=;
  an override (including {}) replaces it and is undone when the subflow
  returns or raises.
- Inheritance holds across asyncio.gather, create_task, .stream(), and a
  subflow called from a tool during generate.
- Reflection v1 and v2 runAction context reaches the flow and a nested
  subflow; v1 spans show auth redacted; v2 rejects a non-object context.

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request introduces comprehensive test suites in flow_context_test.py and reflection_v2_test.py to verify context propagation behavior across flows, subflows, tools, and reflection APIs. The feedback recommends improving test robustness by using defensive guards (e.g., dict(ctx.context or {})) to prevent potential TypeError exceptions when ctx.context is None. Additionally, it is advised to replace pytest.raises inside flow definitions with standard try/except blocks to avoid framework-level exception wrapping that could mask test failures.

@huangjeff5
huangjeff5 requested a review from apascal07 October 6, 2026 21:15
@huangjeff5
huangjeff5 merged commit 795d9a3 into main Oct 7, 2026
25 checks passed
@huangjeff5
huangjeff5 deleted the test/py-action-context-propagation branch October 7, 2026 21:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants