Registry / serialization / lkml
library1.3.7pypypi✓ verified 22d ago

lkml is a speedy LookML parser and serializer implemented in pure Python. It parses LookML strings into Python dictionaries and serializes Python dictionaries back into LookML strings. The library is actively maintained with frequent minor releases to support new LookML syntax and improve parsing/serialization accuracy.

pip install lkml
INSTALL
IMPORT
SIG · LKML
L
lkml
serializationpythonv1.3.7
Install
1.5s avg
Import
101ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.3.7 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.108s · 18MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.5s · import 0.094s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

load
from lkml import load
dump
from lkml import dump

This quickstart demonstrates how to parse a LookML string into a Python dictionary using `lkml.load()`, modify the resulting dictionary, and then serialize it back into a LookML string using `lkml.dump()`. This mirrors the common pattern for programmatic LookML manipulation.

import lkml lookml_content = ''' view: users { sql_table_name: `analytics.users`;; dimension: id { primary_key: yes type: number sql: ${TABLE}.id ;; } } ''' # Parse LookML to a Python dictionary parsed_lookml = lkml.load(lookml_content) print("Parsed LookML:", parsed_lookml) # Modify the dictionary (e.g., change dimension type) for view in parsed_lookml.get('views', []): if view.get('name') == 'users': for dimension in view.get('dimensions', []): if dimension.get('name') == 'id': dimension['type'] = 'string' # Serialize the Python dictionary back to LookML modified_lookml = lkml.dump(parsed_lookml) print("\nModified LookML:\n", modified_lookml)
Debug
Known issues
gotcha`lkml.load()` discards comments and most original whitespace by default. When a dictionary is serialized back to LookML using `lkml.dump()`, the output will follow lkml's opinionated formatting, losing any original comments or custom whitespace. For lossless round-trip parsing that preserves comments and whitespace, direct manipulation of the parse tree (`lkml.tree`) is required.
fix
For simple parsing/serialization, accept the opinionated formatting. For precise control, utilize the lower-level `lkml.parse()` to get a `DocumentNode` and manipulate the parse tree directly, then use `lkml.tree.DocumentNode.to_lookml()`.
affects: All versions
gotcha`lkml.dump()` does not perform semantic validation of the generated LookML. It only guarantees that the output can be successfully parsed by `lkml.load()`. It is the developer's responsibility to ensure the input Python dictionary represents valid LookML constructs to avoid generating malformed or non-functional LookML.
fix
Thoroughly test and validate the structure of the Python dictionaries before serializing them with `lkml.dump()` to ensure they conform to LookML syntax rules.
affects: All versions
breakingIn versions prior to `1.3.1` and `1.3.2`, `lkml.dump()` could unexpectedly mutate the input Python dictionary. This behavior was fixed, preventing unintended side effects on the original data structure.
fix
Upgrade `lkml` to version `1.3.2` or later. If unable to upgrade, pass a copy of the dictionary to `lkml.dump()` (e.g., `lkml.dump(my_dict.copy())`) to prevent mutations of the original object.
affects: < 1.3.1
gotchaLookML keys that can be repeated (e.g., `dimension`, `view`, `join`) are automatically converted into pluralized lists (e.g., `dimensions`, `views`, `joins`) when parsed into a Python dictionary. Special handling exists for keys like `allowed_value` and contents of the `query` block, which might also be lists but are not pluralized.
fix
When working with the parsed Python dictionary, always expect repeated LookML fields to be represented as lists under their pluralized key names. Consult `lkml.keys` for a definitive list of pluralized keys and special cases.
affects: All versions
Errors
Common errors & fixes
SyntaxError: Expected <token type> but found <actual token>
This error occurs when the `lkml.load()` function encounters LookML syntax that does not conform to the expected grammar, often due to typos, missing characters, or incorrect structure in the LookML string or file.
fix
Review the LookML input at the indicated position for any syntax errors. Ensure all blocks are properly closed, attributes are correctly formatted, and all LookML keywords are spelled correctly. The `lkml` documentation on its grammar can be a helpful reference.
TypeError: Value must be a string, list, tuple, or dict.
This error typically arises when using `lkml.dump()` with a Python object that is not a string, list, tuple, or dictionary, which are the supported types for LookML serialization.
fix
Ensure that the Python object passed to `lkml.dump()` is one of the supported types (string, list, tuple, or dictionary) and that its internal structure correctly represents valid LookML. Refer to the 'How LookML is represented by lkml' section in the documentation for guidance on structuring Python objects for serialization.
ModuleNotFoundError: No module named 'lkml'
This error indicates that the `lkml` library has not been installed in your Python environment or the Python interpreter cannot find it in its search paths.
fix
Install the `lkml` library using pip: `pip install lkml`. If you are using a virtual environment, ensure it is activated before installation. If already installed, verify your Python interpreter's path and ensure it's the one with the installed library.
Upgrade
Version history
1.3.7latest on PyPI · released Jan 31, 2025
Audit
Dependencies

No dependency data recorded yet.

Agent activity
5 hits · last 30 days
node
4
Resources