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.
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 atinputs["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 underOUTPUTS_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 upstreamFileRefartifacts are staged locally.OUTPUTS_DIR(str): Writable directory whose files are automatically collected and emitted as downstreamFileRefartifacts 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"):
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:
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, orduckdbrelation as the final expression of a cell (or callingdisplay(df)) renders a sortable, filterable table with column types and null counts. - Charts: Calling
plt.show()withmatplotlibor displaying aplotlyfigure 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 exampleoutputs["row_count"] = len(df)oroutputs["top_segments"] = summary_df.to_dict(orient="records")) rather than assigning raw non-serializable objects.
Debugging Cell Tracebacks
When repairing a failing cell:
- 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). - Guard missing optional keys in
inputswithinputs.get("key", default)so the notebook stays resilient across workflow runs. - 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.
- Keep fixes minimal and localized to the failing cell unless an upstream cell is missing a required import or variable definition.