lsprotocol

2025.0.0 · active · verified Sat Apr 11

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

Install

Imports

Quickstart

This quickstart demonstrates how to create LSP type objects, and how to use the built-in converters to serialize (unstructure) and deserialize (structure) these objects to and from Python dictionaries, typically for JSON communication.

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}")

view raw JSON →