Skip to content

fix sdk session reuse example#225

Closed
acsoto wants to merge 1 commit intovolcano-sh:mainfrom
acsoto:codex/fix-sdk-session-reuse-example
Closed

fix sdk session reuse example#225
acsoto wants to merge 1 commit intovolcano-sh:mainfrom
acsoto:codex/fix-sdk-session-reuse-example

Conversation

@acsoto
Copy link
Copy Markdown
Member

@acsoto acsoto commented Feb 24, 2026

Summary

This PR fixes a misleading Python SDK example for session reuse.

The runtime behavior is that each /api/execute invocation runs in a fresh process. Reusing session_id preserves filesystem state in the same sandbox, but does not preserve in-memory Python variables across separate run_code calls.

User Impact

Users running sdk-python/examples/basic_usage.py could see a NameError in the session reuse section (x not defined) and assume session reuse was broken, even though the actual limitation is process-level execution semantics.

Root Cause

The SDK docs and code comments were already clarified in prior work, but sdk-python/examples/basic_usage.py still demonstrated variable reuse (x = 42 across calls), which contradicted real behavior and the docs.

Fix

Updated sdk-python/examples/basic_usage.py to demonstrate file-based persistence across reused sessions:

  • Changed section wording from generic state persistence to file-system state persistence.
  • Replaced variable-based reuse example with writing /tmp/value.txt in the first client and reading it in the reused client.
  • Updated printed step labels/comments to match actual behavior.

Validation

  • python3 -m py_compile sdk-python/examples/basic_usage.py

Related

@volcano-sh-bot
Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign hzxuzhonghu for approval. For more information see the Kubernetes Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @acsoto, 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 addresses a critical issue in the Python SDK's basic_usage.py example, where the session reuse demonstration was misleading. By clarifying that only file-system state persists across separate run_code calls, the change prevents users from encountering NameError due to incorrect assumptions about in-memory variable persistence. The update ensures the example accurately reflects the interpreter's process-level execution semantics, improving user understanding and preventing frustration.

Highlights

  • Example Clarification: The Python SDK example for session reuse has been updated to accurately reflect that only file-system state persists across run_code invocations, not in-memory Python variables.
  • Misleading Example Fix: The previous example, which demonstrated variable reuse, has been replaced with a file-based persistence example, writing to and reading from /tmp/value.txt.
  • Documentation Alignment: Section wording, printed step labels, and comments in the example have been adjusted to align with the actual behavior and existing documentation clarifications.

🧠 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
  • sdk-python/examples/basic_usage.py
    • Updated the introductory comment to specify 'file-system state persistence' for session reuse.
    • Modified the session_reuse_example function to demonstrate file-based persistence by writing to and reading from /tmp/value.txt.
    • Removed the misleading example of in-memory variable x persistence across run_code calls.
    • Added a note clarifying that each run_code call starts a new process, and Python variables do not persist.
    • Adjusted print statements and comments to accurately describe the file-system state persistence steps.
Activity
  • The pull request was created by acsoto to fix a misleading Python SDK example.
  • The author provided a detailed summary, user impact, root cause, and validation steps in the PR description.
  • No further review comments or activity have been recorded 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.

@acsoto acsoto changed the title [codex] fix sdk session reuse example fix sdk session reuse example Feb 24, 2026
@acsoto acsoto changed the title fix sdk session reuse example [codex] fix sdk session reuse example Feb 24, 2026
@acsoto
Copy link
Copy Markdown
Member Author

acsoto commented Feb 24, 2026

Superseded by #226 (same change with branch/title/template aligned). Closing this one to avoid confusion.

@acsoto acsoto closed this Feb 24, 2026
@acsoto acsoto changed the title [codex] fix sdk session reuse example fix sdk session reuse example Feb 24, 2026
Copy link
Copy Markdown
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 effectively corrects a misleading example regarding session reuse in the Python SDK. By changing the demonstration from in-memory variable persistence (which doesn't work) to file-system persistence, the example now accurately reflects the SDK's behavior. The accompanying updates to comments and documentation are clear and helpful for users. The change is a valuable improvement for the SDK's usability and correctness.

client2 = CodeInterpreterClient(session_id=session_id, verbose=True)
result = client2.run_code("python", "print(f'x = {x}')")
print(f"Result: {result.strip()}") # Should print "x = 42"
result = client2.run_code("python", "print(open('/tmp/value.txt').read())")
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.

medium

While this code works for this simple case, it's a best practice in Python to use a with statement when dealing with files. This ensures the file is properly closed even if errors occur. Using with open(...) is more robust and idiomatic, which is a good pattern to show in an example.

Suggested change
result = client2.run_code("python", "print(open('/tmp/value.txt').read())")
result = client2.run_code("python", "with open('/tmp/value.txt') as f: print(f.read())")

@acsoto acsoto deleted the codex/fix-sdk-session-reuse-example branch February 24, 2026 09:08
@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Feb 24, 2026

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 41.70%. Comparing base (845b798) to head (a91ef35).
⚠️ Report is 109 commits behind head on main.
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #225      +/-   ##
==========================================
+ Coverage   35.60%   41.70%   +6.09%     
==========================================
  Files          29       30       +1     
  Lines        2533     2611      +78     
==========================================
+ Hits          902     1089     +187     
+ Misses       1505     1390     -115     
- Partials      126      132       +6     
Flag Coverage Δ
unittests 41.70% <ø> (+6.09%) ⬆️

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.

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.

3 participants