Skip to content

Add access to pipeline options during pipeline construction and translation.#37522

Open
claudevdm wants to merge 4 commits intoapache:masterfrom
claudevdm:poptions-context
Open

Add access to pipeline options during pipeline construction and translation.#37522
claudevdm wants to merge 4 commits intoapache:masterfrom
claudevdm:poptions-context

Conversation

@claudevdm
Copy link
Collaborator

This adds pipeline option access via context vars in a thread/concurrency safe way.

Pipeline options are available via context manager during pipeline.apply and pipeline.run.


Thank you for your contribution! Follow this checklist to help us incorporate your contribution quickly and easily:

  • Mention the appropriate issue in your description (for example: addresses #123), if applicable. This will automatically add a link to the pull request in the issue. If you would like the issue to automatically close on merging the pull request, comment fixes #<ISSUE NUMBER> instead.
  • Update CHANGES.md with noteworthy changes.
  • If this contribution is large, please file an Apache Individual Contributor License Agreement.

See the Contributor Guide for more tips on how to make review process smoother.

To check the build health, please visit https://github.com/apache/beam/blob/master/.test-infra/BUILD_STATUS.md

GitHub Actions Tests Status (on master branch)

Build python source distribution and wheels
Python tests
Java tests
Go tests

See CI.md for more information about GitHub Actions CI or the workflows README to see a list of phrases to trigger workflows.

@github-actions github-actions bot added the python label Feb 5, 2026
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @claudevdm, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request enhances the Apache Beam Python SDK by introducing a robust mechanism for managing PipelineOptions in concurrent environments. By leveraging Python's contextvars, it ensures that pipeline options are correctly isolated and accessible in a thread-safe and async-safe manner throughout the pipeline's construction and execution lifecycle. This change addresses potential race conditions and improves the reliability of option retrieval within complex pipeline definitions and transformations.

Highlights

  • Introduction of Context-Scoped Pipeline Options: A new module pipeline_construction_options.py is added to provide thread-safe and async-safe access to PipelineOptions using Python's contextvars.
  • scoped_pipeline_options Context Manager: A context manager is introduced to set and manage PipelineOptions for the duration of a code block, ensuring proper restoration of previous options in nested scenarios.
  • Integration with Pipeline.run() and Pipeline.apply(): The Pipeline.run() and Pipeline.apply() methods are updated to utilize the new scoped_pipeline_options context manager, making pipeline options accessible during both pipeline construction and execution phases.
  • Comprehensive Testing: Extensive unit tests are added to pipeline_construction_options_test.py to validate the correct isolation of pipeline options across threads and asyncio tasks, as well as proper behavior in nested scopes and during transform expansion and coder proto conversion.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • sdks/python/apache_beam/options/pipeline_construction_options.py
    • Added a new module to provide context-scoped access to PipelineOptions.
    • Introduced _current_pipeline_options as a ContextVar to store options.
    • Implemented get_current_pipeline_options() to retrieve the current options.
    • Implemented scoped_pipeline_options() as a context manager for setting options.
  • sdks/python/apache_beam/options/pipeline_construction_options_test.py
    • Added a new test file to verify the functionality of pipeline_construction_options.
    • Included tests for nested scoping, exception handling, thread isolation, and asyncio task isolation.
    • Added tests to confirm that transforms and coders can correctly access pipeline options during their respective phases.
  • sdks/python/apache_beam/pipeline.py
    • Imported scoped_pipeline_options from the new module.
    • Refactored Pipeline.run() to wrap its logic in scoped_pipeline_options(self._options) and delegate to a new _run_internal() method.
    • Refactored Pipeline.apply() to wrap its logic in scoped_pipeline_options(self._options) and delegate to a new _apply_internal() method.
Activity
  • The pull request was created by claudevdm.
  • No human activity (comments, reviews, or progress updates) has been recorded on this pull request yet.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@claudevdm
Copy link
Collaborator Author

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

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 a thread-safe and concurrency-safe mechanism for accessing pipeline options during pipeline construction and translation, using contextvars. The changes are well-implemented and include a comprehensive set of tests to validate the new functionality, especially in multi-threaded and asynchronous scenarios. The core logic is encapsulated in a new pipeline_construction_options module, and the Pipeline class is updated to use it. I have one suggestion to improve efficiency by avoiding nested context managers in a recursive call within the _apply_internal method. Overall, this is a valuable addition that improves the robustness of the pipeline execution context.

@claudevdm claudevdm marked this pull request as ready for review February 5, 2026 17:49
@claudevdm
Copy link
Collaborator Author

R: @tvalentyn

@claudevdm claudevdm requested a review from tvalentyn February 5, 2026 17:49
@github-actions
Copy link
Contributor

github-actions bot commented Feb 5, 2026

Stopping reviewer notifications for this pull request: review requested by someone other than the bot, ceding control. If you'd like to restart, comment assign set of reviewers

@codecov
Copy link

codecov bot commented Feb 5, 2026

Codecov Report

❌ Patch coverage is 91.30435% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 40.07%. Comparing base (4e76198) to head (36d1fbb).
⚠️ Report is 4 commits behind head on master.

Files with missing lines Patch % Lines
...ache_beam/options/pipeline_construction_options.py 92.30% 1 Missing ⚠️
sdks/python/apache_beam/pipeline.py 90.00% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master   #37522      +/-   ##
============================================
+ Coverage     40.06%   40.07%   +0.01%     
  Complexity     3404     3404              
============================================
  Files          1177     1178       +1     
  Lines        187083   187195     +112     
  Branches       3581     3581              
============================================
+ Hits          74947    75012      +65     
- Misses       108744   108791      +47     
  Partials       3392     3392              
Flag Coverage Δ
python 39.71% <91.30%> (+0.02%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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.

1 participant