Skip to main content

NotebookSession

The run() method returns a NotebookSession object containing all executed code cells and their results:
session = await agent.run()

# Access basic properties
session.id                    # Unique session identifier
session.cells                 # List of CodeCell objects
session.current_execution_count  # Total number of executed cells
session.namespace             # Dictionary with all variables from execution

# Helper methods
session.get_cell(cell_id)     # Get a specific cell by ID
session.get_latest_cell()     # Get the most recently executed cell

CodeCell Properties

Each cell in session.cells represents one executed code block:
for cell in session.cells:
    cell.id              # Unique cell identifier
    cell.cell_type       # 'code' or 'markdown'
    cell.source          # The code that was executed
    cell.output          # Output from code execution (if any)
    cell.execution_count # Execution order number
    cell.status          # 'pending', 'running', 'success', or 'error'
    cell.error           # Error message if execution failed
    cell.browser_state   # Browser state after execution

Accessing Results

Basic Usage

session = await agent.run()

# Iterate through all executed cells
for cell in session.cells:
    print(f"Cell {cell.execution_count}:")
    print(f"  Code: {cell.source}")
    if cell.error:
        print(f"  Error: {cell.error}")
    elif cell.output:
        print(f"  Output: {cell.output}")
    print(f"  Status: {cell.status}")

# Get the last cell
last_cell = session.get_latest_cell()
if last_cell:
    print(f"Last output: {last_cell.output}")

# Access variables from the execution namespace
products = session.namespace.get('products', [])
print(f"Extracted {len(products)} products")

Checking Task Completion

When the agent calls done(), the result is stored in the namespace:
session = await agent.run()

# Check if task was completed
task_done = session.namespace.get('_task_done', False)
task_result = session.namespace.get('_task_result')
task_success = session.namespace.get('_task_success')

if task_done:
    print(f"Task completed: {task_success}")
    print(f"Result: {task_result}")

Getting All Outputs

session = await agent.run()

# Get all outputs (excluding errors)
outputs = [cell.output for cell in session.cells if cell.output]

# Get all errors
errors = [cell.error for cell in session.cells if cell.error]

# Get successful cells only
successful_cells = [cell for cell in session.cells if cell.status == 'success']

Data Models

See the complete data model definitions in the CodeAgent views source code.