{"record":{"id":"51890abfd56676cb","repo":"streamlit/streamlit","slug":"main-dg-is-not-set","errorCode":null,"errorMessage":"main_dg is not set","messagePattern":"main_dg is not set","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"lib/streamlit/delta_generator_singletons.py","lineNumber":236,"sourceCode":"        return self._ensure_context_var().__hash__()\n\n\n# we don't use the default factory here because `main_dg` is not initialized when this\n# module is imported. This is why we have our own ContextVar wrapper.\ncontext_dg_stack: ContextVarWithLazyDefault[tuple[DeltaGenerator, ...]] = (\n    ContextVarWithLazyDefault(\n        \"context_dg_stack\", default=lambda: (get_dg_singleton_instance().main_dg,)\n    )\n)\n\n\ndef get_default_dg_stack_value() -> tuple[DeltaGenerator, ...]:\n    \"\"\"Get the default dg_stack value with which the dg_stack should\n    be initialized and reset if needed.\n    \"\"\"\n    instance = get_dg_singleton_instance()\n    if instance._main_dg is None:\n        raise RuntimeError(\"main_dg is not set\")\n\n    return (instance._main_dg,)\n\n\ndef get_last_dg_added_to_context_stack() -> DeltaGenerator | None:\n    \"\"\"Get the last added DeltaGenerator of the stack in the current context.\n\n    Returns None if the stack has only one element or is empty for whatever reason.\n    \"\"\"\n    current_stack = context_dg_stack.get()\n    # If set to \"> 0\" and thus return the only delta generator in the stack -\n    # which logically makes more sense -, some unit tests fail.\n    # It looks like the reason is that they create their own main delta generator\n    # but do not populate the dg_stack correctly. However, to be on the safe-side,\n    # we keep the logic but leave the comment as shared knowledge for whoever will look\n    # into this in the future.\n    if len(current_stack) > 1:\n        return current_stack[-1]","sourceCodeStart":218,"sourceCodeEnd":254,"githubUrl":"https://github.com/streamlit/streamlit/blob/202661fc552db98c2ba0bb0e113d619689baf9de/lib/streamlit/delta_generator_singletons.py#L218-L254","documentation":"Error \"main_dg is not set\" thrown in streamlit/streamlit.","triggerScenarios":"Thrown at lib/streamlit/delta_generator_singletons.py:236 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":[],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"202661fc552db98c2ba0bb0e113d619689baf9de","analyzedAt":"2026-08-26T09:13:21.291Z","schemaVersion":2},"datasetVersion":"2026-08-26T14:46:13.012Z"}