Registry / serialization / docspec

docspec

JSON →
library2.2.2pypypi✓ verified 86d ago

Docspec is a JSON object specification for representing API documentation of programming languages. While in its current form it primarily targets Python APIs, it was designed to be extensible to cover a more generalized set of languages. It also provides a Python module for the (de-)serialization and in-memory modification of docspec-conformant JSON payloads. The current version is 2.2.2 and it follows a feature-driven release cadence.

pip install docspec
INSTALL
IMPORT
SIG · DOCSPEC
D
docspec
serializationpythonv2.2.2
Install
2.5s avg
Import
209ms
Disk
18MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v2.2.2 · 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.920 runs
installs and imports cleanly · install 0.0s · import 0.222s · 20MB
glibc
py 3.103.920 runs
installs and imports cleanly · install 2.5s · import 0.197s · 21MB
18MB installed
● package 18MB
Code
Verified usage

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

docspec
import docspec

This quickstart demonstrates how to load a docspec-conformant JSON payload, access its structured API elements (like modules and functions), and perform basic modifications. It highlights docspec's role in handling the JSON representation of API documentation.

import docspec from io import StringIO # Example of a docspec-conformant JSON payload for a simple module # In a real-world scenario, this might come from a file or from docspec-python. example_json_input = ''' { "name": "example_module", "location": {"filename": "example_module.py", "lineno": 1}, "docstring": "A module demonstrating docspec structure.", "members": [ { "name": "my_function", "type": "function", "location": {"filename": "example_module.py", "lineno": 3}, "docstring": "This is a sample function.", "args": [], "returns": null, "decorations": [], "is_async": false } ] } ''' # Load module(s) from a JSON string (simulating file input) modules = docspec.load_modules(StringIO(example_json_input)) # Iterate and process loaded modules for module in modules: print(f"\nProcessing Module: {module.name}") print(f" Module Docstring: {module.docstring}") # Example: Filter members to only include those with docstrings original_members_count = len(module.members) module.members = [member for member in module.members if member.docstring] print(f" Original member count: {original_members_count}") print(f" Members with docstrings after filtering: {[m.name for m in module.members]}") # To dump the modified module back to JSON (e.g., to stdout or a file): # from sys import stdout # for module in modules: # Assuming 'modules' might have multiple # docspec.dump_module(stdout, module)
Debug
Known issues
breakingThe official GitHub repository for 'docspec' was moved from `NiklasRosenstein/docspec` to `NiklasRosenstein/python-docspec` on May 13, 2023. Old links or git clone commands pointing to the former URL will no longer work.
fix
Update repository URLs to `https://github.com/NiklasRosenstein/python-docspec`.
affects: All versions
gotchaThere are two related packages: `docspec` and `docspec-python`. Users often confuse them. `docspec` provides the specification and library for handling the JSON data structure. `docspec-python` is a *parser* that takes actual Python source code and generates `docspec`-conformant JSON data. You typically need both if you want to extract docs from Python code.
fix
If you need to parse Python source code into docspec format, install `docspec-python` (`pip install docspec-python`). `docspec-python` depends on `docspec`, so `docspec` will be installed automatically.
affects: All versions
gotcha`docspec` itself (the core library) does not contain functionality to parse Python source code or any other programming language. Its purpose is to deserialize, work with, and serialize the language-agnostic Docspec JSON format.
fix
To parse Python source code into Docspec objects, you must use the `docspec-python` companion library. For other languages, you would need a specific parser for that language that outputs Docspec JSON.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'docspec'
The 'docspec' package is not installed in your Python environment or is not accessible via the Python path.
fix
pip install docspec
ImportError: cannot import name 'Docstring' from 'docspec.models'
The 'Docstring' class is typically exposed directly under the 'docspec' module, not within a submodule like 'docspec.models'.
fix
from docspec import Docstring
AttributeError: 'Docstring' object has no attribute 'text'
You are trying to access a non-existent attribute named 'text' on a `docspec.Docstring` object. The primary attribute for its content is usually `content`.
fix
docstring_object.content
Upgrade
Version history
2.2.2latest on PyPI · released May 6, 2025
Audit
Dependencies
pythonrequiredRuntime environment for the Python library.
Agent activity
18 hits · last 30 days
node
16
OpenAI (training)
1
Resources
docspec — pip install docspec · libregistry