Skip to content

Conversation

@sundargthb
Copy link
Contributor

  • Make session_name parameter optional in all action models
  • Implement auto-session mode with _ensure_session() method
  • Enable automatic session creation when sessions don't exist
  • Update tool description to prioritize common use cases
  • Improve developer experience by reducing manual session management

Description

This PR introduces automatic session management for the Code Interpreter tool, significantly improving the developer and LLM user experience by eliminating the need for manual session initialization in common use cases.

Motivation:
Current implementation requires explicit session initialization before any code execution:

  1. LLMs must make two separate tool calls (initSession, then executeCode)
  2. Adds unnecessary complexity for simple code execution tasks
  3. Increases latency and error potential
  4. Creates cognitive overhead when designing agent workflows

Key Changes:

  1. Optional session_name Parameter (models.py)

    • Made session_name optional with None default in all action models
    • Updated field descriptions to indicate auto-creation behavior
    • Maintains backward compatibility with explicit session names
  2. Automatic Session Management (agent_core_code_interpreter.py)

    • Added auto_session parameter (defaults to True) to enable/disable automatic session creation
    • Added default_session parameter (defaults to "default") for unnamed sessions
    • Implemented _ensure_session() method to handle session lifecycle
    • Updated all execution methods to use this mechanism
  3. Improved Tool Description (code_interpreter.py)

    • Restructured description to prioritize common use cases
    • Added clear examples of simple code execution without session management

Before vs After:

# BEFORE: Required two operations
interpreter.code_interpreter({
    "action": {"type": "initSession", "session_name": "my-session", "description": "..."}
})
interpreter.code_interpreter({
    "action": {"type": "executeCode", "session_name": "my-session", "code": "print('hello')"}
})

# AFTER: Single operation - session auto-created
interpreter.code_interpreter({
    "action": {"type": "executeCode", "code": "print('hello')"}
})

Related Issues

Addresses developer feedback regarding Code Interpreter tool complexity and performance concerns in multi-step agent workflows.

Documentation PR

N/A - Tool description has been updated inline. No additional documentation changes required at this time.

Type of Change

Enhancement to Existing Tool

Testing

  • Unit & Integration Tests:

    • Updated all test files to work with the new auto-session behavior
    • Added new tests specifically for the auto-session creation feature
    • Ensured backward compatibility is maintained and tested
  • Local Testing:

    • Verified automatic session creation works correctly
    • Confirmed optional session_name parameter functions as expected
    • Tested that existing explicit session management still works
    • Validated error handling when auto_session is disabled
  • Performance Impact:

    • Reduced tool call count from 5-7 to 1-2 for simple operations
    • Eliminated ~8-30 seconds of session initialization overhead
    • Improved LLM success rate by simplifying tool usage pattern Verify that the changes do not break functionality or introduce warnings in consuming repositories: agents-docs, agents-tools, agents-cli
  • [X ] I ran hatch run prepare

Checklist

  • [X ] I have read the CONTRIBUTING document
  • [X ] I have added any necessary tests that prove my fix is effective or my feature works
  • [X ] I have updated the documentation accordingly
  • [X ] I have added an appropriate example to the documentation to outline the feature
  • [X ] My changes generate no new warnings
  • [X ] Any dependent changes have been merged and published

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

- Make session_name parameter optional in all action models
- Implement auto-session mode with _ensure_session() method
- Enable automatic session creation when sessions don't exist
- Update tool description to prioritize common use cases
- Improve developer experience by reducing manual session management
@sundargthb sundargthb requested a review from a team as a code owner October 15, 2025 02:18
@cagataycali cagataycali enabled auto-merge (squash) October 15, 2025 02:25
auto-merge was automatically disabled October 15, 2025 02:30

Head branch was pushed to by a user without write access

cagataycali
cagataycali previously approved these changes Oct 15, 2025
…ved large tool description formatting changes from this PR
dbschmigelski
dbschmigelski previously approved these changes Oct 20, 2025
JackYPCOnline
JackYPCOnline previously approved these changes Oct 20, 2025
@cagataycali cagataycali enabled auto-merge (squash) October 20, 2025 23:38
@dbschmigelski
Copy link
Member

@sundargthb

Looks like there are some test failures, is there something needed in the test account to enable these?

FAILED tests_integ/code_interpreter/test_agent_core_code_interpreter_custom_identifier.py::TestIdentifierValidationAndEdgeCases::test_auto_create_flag_behavior - ValueError: Session 'non-existent-session' not found. Create it first using initSession.
FAILED tests_integ/code_interpreter/test_agent_core_code_interpreter_custom_identifier.py::TestErrorScenariosWithIdentifierContext::test_session_not_found_error_with_custom_identifier - ValueError: Session 'non-existent-session' not found. Create it first using initSession.

auto-merge was automatically disabled October 21, 2025 20:14

Head branch was pushed to by a user without write access

@sundargthb
Copy link
Contributor Author

@sundargthb

Looks like there are some test failures, is there something needed in the test account to enable these?

FAILED tests_integ/code_interpreter/test_agent_core_code_interpreter_custom_identifier.py::TestIdentifierValidationAndEdgeCases::test_auto_create_flag_behavior - ValueError: Session 'non-existent-session' not found. Create it first using initSession.
FAILED tests_integ/code_interpreter/test_agent_core_code_interpreter_custom_identifier.py::TestErrorScenariosWithIdentifierContext::test_session_not_found_error_with_custom_identifier - ValueError: Session 'non-existent-session' not found. Create it first using initSession.

@dbschmigelski - i noticed that and i just updated the integration test file and it passes locally.. can you do manual approval so that it proceeds with the integration test

@cagataycali cagataycali merged commit 2bc0624 into strands-agents:main Oct 22, 2025
31 of 32 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants