context

Page actions AI-ready formats and sharing
Open LLM text
Share with AI
Ask Claude Ask ChatGPT Ask Gemini Ask Copilot

Request-scoped context via ContextVar.

Provides:

  • request_var: The current Requestfor this task/thread.
  • g: A mutable namespace scoped to the current request.

Both are set by the handler pipeline and reset…

Request-scoped context via ContextVar.

Provides:

  • request_var: The current Requestfor this task/thread.
  • g: A mutable namespace scoped to the current request.

Both are set by the handler pipeline and reset after each request. They are explicitly opt-in — if no middleware or handler sets them, accessing them raisesLookupError.

Thread safety:

``ContextVar`` is task-local under asyncio and thread-local under
free-threading (3.14t). No locks needed.

context

Name Type Default Description
type —
qualified_name —
element_type —
description —
source_file —
line_number —
is_autodoc —
autodoc_element —
_autodoc_template —
_autodoc_url_path —
_autodoc_page_type —
title —
doc_content_hash —

Symbols on this page

get_request
function
def get_request() -> Request

Return the current request.

RaisesLookupErrorif called outside a request context.

No parameters.

_RequestGlobals
class

A mutable namespace scoped to the current request.

Inspired by Flask'sg. Stores arbitrary attributes via a per-request dict held in a ContextVar.

Usage::

from chirp.context import g

# In middleware
g.user = current_user

# In handler
name = g.user.name

View source · /home/runner/work/chirp/chirp/site/../src/chirp/context.py:1