Code variables in Python
Contents
The Python SDK (v6.9.1+) supports automatic code variables capture, allowing you to see the state of local variables when exceptions occur.
Enabling code variables capture
Code variables capture can be enabled globally during PostHog initialization:
Scoped capture with contexts
You can enable or disable code variables capture for specific code blocks using contexts:
Configuration options
Masking sensitive variables
Variable names matching mask patterns will have their values replaced with *** in the UI.
The SDK includes sensible defaults for common sensitive variable names:
Customizing mask patterns
Scoped mask patterns
You can set mask patterns for specific code blocks using contexts:
Detecting secrets automatically
As a last resort, the SDK also scans captured variable values for high-entropy secrets that name-based masking misses — API keys, tokens, and strong passwords in innocuously-named variables — and replaces them with ***. It recognizes common key formats (OpenAI, Anthropic, AWS, Stripe, GitHub, and more) and random high-entropy strings, while leaving identifiers like UUIDs, hashes, file paths, and URLs untouched.
This is enabled by default. To disable it globally:
Or for a specific code block using contexts:
Masking connection string credentials
Credentials embedded in connection strings and URLs are scrubbed automatically, regardless of the variable name. Only the credentials are replaced — the scheme, host, and path are kept so the value stays useful for debugging:
This is enabled by default. To disable it globally:
Or for a specific code block using contexts:
Ignoring variables
Variable names matching ignore patterns are not captured at all. This is useful for excluding internal variables, temporary data, or framework-specific variables that don't provide debugging value.
By default, Python internal variables (starting with __) are ignored.
Customizing ignore patterns
Scoped ignore patterns
You can set ignore patterns for specific code blocks using contexts: