▶ runtime-narrative

Examples

Every script under examples/ is runnable as-is: uv run python examples/<name>.py.

Core

ScriptDemonstrates
success.pyMinimal story()/stage() API, no decorators, a success path
basic.py@runtime_narrative_story/@runtime_narrative_stage decorators, a failure path
basic_ollama.pySame failure path with OllamaFailureAnalyzer attached

Sub-stories and logging (newest features)

ScriptDemonstrates
substory_db_call.pyNested story() auto-linking as a sub-story (API call → DB call)
logging_bridge.pyNarrativeLogHandler folding logging calls into the story pipeline
structured_log_routing.pyextra= fields, level_icons, and FilteredRenderer per-story-family routing

Auto-instrumentation

ScriptDemonstrates
narrative_class.py@narrative_class and @no_stage
instrument_module.pyinstrument_module() on an existing module
auto_instrument.pyauto_instrument() import-hook, zero call-site changes

Failure diagnostics and analysis

ScriptDemonstrates
diagnostics_config.pyFailureDiagnosticsConfig — rich mode, redaction, production caps
background_analysis.pybackground_analysis=True — non-blocking LLM analysis
anthropic_analyzer.pyAnthropicFailureAnalyzer + DeduplicatingAnalyzer

Renderers and observability

ScriptDemonstrates
html_report.pyHtmlReportRenderer — self-contained HTML report
sqlite_persistence.pySqliteStoryRenderer + the runtime-narrative CLI
otel_tracing.pyOtelRenderer, OtelLogRenderer, OtelMetricsRenderer
alert_routing.pyAlertRoutingRenderer — async webhook fan-out
colorful_errors_and_emojis.pyConsoleRenderer's built-in color + level_icons emoji across log levels and a failure

Framework integrations and concurrency

ScriptDemonstrates
middleware_skip_if.pyRuntimeNarrativeMiddleware(skip_if=...) for FastAPI/Starlette
task_group.pyNarrativeTaskGroup — concurrent asyncio tasks under one story
fastapi_app/Full FastAPI demo app (uv run python -m examples.fastapi_app.run)
fastapi_ugly_traceback_demo.pyThe same deep async bug (route → orchestrator → retry-wrapped pricing engine → asyncio.gather fan-out → the actual TypeError), run once with no instrumentation and once with runtime-narrative — a raw ~35-frame traceback vs. one pinpointed line, source snippet, and locals

Testing and lifecycle utilities

ScriptDemonstrates
story_recorder.pyStoryRecorder test assertion API
dry_run_mode.pydry_run=True — verify wiring with no side effects
optional_stage.pyhas_active_story() and stage(optional=True)
saga_record_failure.pyStoryRuntime.record_failure() in a saga/rollback flow
stage_story_name.pystory_name on StageStarted/StageCompleted

Start from the top

New to runtime-narrative? Head back to the homepage for the overview, or Installation to get the package into your project and start wrapping code in story().