Skip to main content

LLM4B Python Notebook Authoring Guide

Author, debug, and refactor Python notebooks in LLM4B Notebook Studio using workflow inputs, outputs, FileRef payloads, Project Datasets, and rich table/chart conventions.

Notebook Authoring

LLM4B Python Notebook Authoring Guide

Use this skill when writing, fixing, or refactoring Python cells inside an LLM4B Python Notebook (action_notebook) node.

Runtime Globals & Workflow Contract

Every notebook executes inside an Isolated Sandbox with these pre-populated globals:

  • inputs (dict): Workflow input payload and upstream node outputs wired into the notebook. If the node configures an Input State Key, its value is also aliased at inputs["input"].
  • outputs (dict): Mutable dictionary for structured wire outputs passed to downstream workflow nodes under the node's Output State Key. Assign JSON-serializable values (dict, list, str, int, float, bool, None) or generated file paths written under OUTPUTS_DIR.
  • datasets (dict): Project or Workspace-shared Datasets attached during interactive session hydration (mapping dataset name to staged local file paths and schema metadata).
  • INPUTS_DIR (str): Read-only directory where upstream FileRef artifacts are staged locally.
  • OUTPUTS_DIR (str): Writable directory whose files are automatically collected and emitted as downstream FileRef artifacts when the notebook finishes.
  • DATASETS_DIR (str): Read-only directory containing hydrated Dataset files.

Working with FileRef Objects

Upstream files arrive in inputs as structured FileRef dictionaries ("$llm4b_type": "file_ref"):

Code
from pathlib import Path
import pandas as pd

file_ref = inputs.get("report_file")
if isinstance(file_ref, dict) and file_ref.get("$llm4b_type") == "file_ref":
    local_path = Path(file_ref["local_path"])
    df = pd.read_parquet(local_path)

To emit a file for downstream workflow nodes, write it into OUTPUTS_DIR:

Code
from pathlib import Path

out_path = Path(OUTPUTS_DIR) / "summary.csv"
df.to_csv(out_path, index=False)
outputs["summary_file"] = str(out_path)

Pre-Installed Data Science & Visualization Libraries

Prefer the pre-installed analytical stack so notebooks run deterministically with zero external package installs:

  • Tabular & SQL: pandas, polars, duckdb, pyarrow, numpy, scipy
  • Machine Learning: scikit-learn
  • Charts & Plots: matplotlib (inline backend pre-configured), plotly, pillow

Rich Outputs in Notebook Studio & Run History

  • Interactive Tables: Leaving a pandas.DataFrame, polars.DataFrame, or duckdb relation as the final expression of a cell (or calling display(df)) renders a sortable, filterable table with column types and null counts.
  • Charts: Calling plt.show() with matplotlib or displaying a plotly figure renders an isolated visual plot embedded in the executed notebook report.
  • Downstream Wire Outputs: Always write concise summary metrics or structured records to outputs["..."] (for example outputs["row_count"] = len(df) or outputs["top_segments"] = summary_df.to_dict(orient="records")) rather than assigning raw non-serializable objects.

Debugging Cell Tracebacks

When repairing a failing cell:

  1. Check the exact column names and data types from the live kernel variables and attached Dataset schemas before indexing DataFrames (KeyError, dtype mismatches, or null values).
  2. Guard missing optional keys in inputs with inputs.get("key", default) so the notebook stays resilient across workflow runs.
  3. Never hardcode API keys, tokens, or passwords in notebook cells—use the workspace Secrets Vault and dedicated AI/HTTP workflow nodes for authenticated external calls.
  4. Keep fixes minimal and localized to the failing cell unless an upstream cell is missing a required import or variable definition.