{"record":{"id":"232a7222b0594841","repo":"cocoindex-io/cocoindex","slug":"no-componentcontext-available-this-function-must","errorCode":null,"errorMessage":"No ComponentContext available. This function must be called from within an active component context (inside a mount/use_mount call or App.update).","messagePattern":"No ComponentContext available\\. This function must be called from within an active component context \\(inside a mount/use_mount call or App\\.update\\)\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"python/cocoindex/_internal/component_ctx.py","lineNumber":356,"sourceCode":"        ExceptionHandlerChain(handler=env.exception_handler)\n        if env.exception_handler\n        else None\n    )\n    context = ComponentContext(env, path, comp_ctx, fn_ctx, base_chain)\n    tok = _context_var.set(context)\n    try:\n        yield\n    finally:\n        _context_var.reset(tok)\n        comp_ctx.join_fn_call(fn_ctx)\n\n\ndef get_context_from_ctx() -> ComponentContext:\n    \"\"\"Get the current ComponentContext from ContextVar.\"\"\"\n    ctx_var = _context_var.get(None)\n    if ctx_var is not None:\n        return ctx_var\n    raise RuntimeError(\n        \"No ComponentContext available. This function must be called from within \"\n        \"an active component context (inside a mount/use_mount call or App.update).\"\n    )\n\n\ndef build_child_path(\n    parent_ctx: ComponentContext, subpath: ComponentSubpath\n) -> core.StablePath:\n    \"\"\"Build the child path from parent context and subpath.\"\"\"\n    child_path = parent_ctx._core_path\n    for part in subpath.parts:\n        child_path = child_path.concat(part)\n    return child_path\n\n\ndef use_context(key: ContextKey[T]) -> T:\n    \"\"\"\n    Retrieve a value from the context.","sourceCodeStart":338,"sourceCodeEnd":374,"githubUrl":"https://github.com/cocoindex-io/cocoindex/blob/e84aa99b3292c5270a4b313b2a7137ad9ce8ab3b/python/cocoindex/_internal/component_ctx.py#L338-L374","documentation":"get_context_from_ctx() fetches the ambient ComponentContext from a ContextVar that is only set while a component is executing (inside mount/use_mount or App.update). Called outside such a scope there is no context, and the function raises RuntimeError explaining where the call is valid.","triggerScenarios":"Calling context-dependent APIs (coco.use_context, coco.use_state, nested mount/use_mount, get_context_from_ctx) from module top level, plain functions outside a mounted component, a background thread without ctx.attach(), or after the component finished.","commonSituations":"Calling coco.use_context() at import time; running coco APIs in a ThreadPoolExecutor without attaching the component context; invoking helpers from tests outside an App.update; fire-and-forget tasks that outlive the component.","solutions":["Call the API from inside a mounted component body (function passed to mount/use_mount/mount_each) or during App.update","For threads, grab ctx = coco.get_component_context() in the component and wrap thread work in `with ctx.attach():`","For background work, keep it within the component's async scope (e.g. create tasks from inside the component rather than after it)"],"exampleFix":"// before\ndb = coco.use_context(PG_DB)  # module level -> RuntimeError\n// after\n@coco.fn\nasync def app_main(...):\n    db = coco.use_context(PG_DB)","handlingStrategy":"type-guard","validationCode":"import cocoindex as coco\n\ndef has_component_ctx() -> bool:\n    try:\n        coco.get_component_context()\n        return True\n    except RuntimeError:\n        return False","typeGuard":"def in_component_context() -> bool:\n    try:\n        coco.get_component_context()\n        return True\n    except RuntimeError:\n        return False","tryCatchPattern":"try:\n    db = coco.use_context(PG_DB)\nexcept RuntimeError as e:\n    if \"No ComponentContext available\" in str(e):\n        raise  # move the call inside a mounted component / attach ctx in threads","preventionTips":["Only call coco.use_context/use_state/mount-family APIs inside component bodies","In worker threads, wrap coco calls in `with coco.get_component_context().attach():`","Never call these APIs at import time or from detached tasks that outlive the component"],"tags":["python","context","lifecycle"],"backgroundTag":"invalid-state-transition","analyzedSha":"e84aa99b3292c5270a4b313b2a7137ad9ce8ab3b","analyzedAt":"2026-09-08T15:59:19.997Z","contentChangedAt":"2026-09-08T15:59:19.997Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}