lsprotocol
lsprotocol is a Python library that provides generated type definitions for the Language Server Protocol (LSP). It allows developers to build language servers and clients by providing a robust and easy-to-use type generation system, staying up-to-date with the latest LSP specification. The library follows a yearly major release cadence.
Warnings
- breaking Breaking changes in type names were introduced due to updates to the latest LSP specification. Starting with versions like `2024.0.0a2`, many type names were standardized, often prefixing methods/types with `TextDocument` or similar.
- breaking Further type name standardization occurred around `2025.x` versions, aiming to remove overly specific or long type names (e.g., `NotebookDocumentSyncRegistrationOptionsNotebookSelectorType2CellsType`) in favor of more consistent naming.
- gotcha Optional fields in LSP types, when not explicitly set, might be unstructured as `None` in the resulting dictionary/JSON, rather than being omitted entirely. Some LSP servers might not tolerate `null` for optional parameters and expect them to be absent.
- gotcha lsprotocol updates regularly to align with new versions of the Language Server Protocol specification. Using a specific version of `lsprotocol` means you are bound to the LSP spec it implements. New LSP features or changes in existing types are introduced with spec updates.
Install
-
pip install lsprotocol
Imports
- types
from lsprotocol import types
- converters
from lsprotocol import converters
Quickstart
import json
from lsprotocol import converters, types
# Create an LSP Position object
position = types.Position(line=10, character=3)
print(f"Created Position object: {position}")
# Get a converter instance
converter = converters.get_converter()
# Unstructure (serialize) the object to a dictionary (suitable for JSON)
lsp_dict = converter.unstructure(position, unstructure_as=types.Position)
print(f"Unstructured to dictionary: {lsp_dict}")
# Convert to JSON string
json_output = json.dumps(lsp_dict)
print(f"JSON output: {json_output}")
# Structure (deserialize) a dictionary back into an LSP object
data_from_json = {'line': 5, 'character': 0}
structured_position = converter.structure(data_from_json, types.Position)
print(f"Structured back to object: {structured_position}")