Skip to content

Commit 1fcce65

Browse files
feat: add python sdk
1 parent 7e6629f commit 1fcce65

File tree

14 files changed

+1121
-0
lines changed

14 files changed

+1121
-0
lines changed

.github/workflows/python-ci.yml

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
name: Python CI
2+
3+
on:
4+
push:
5+
branches: [main, master]
6+
paths:
7+
- "packages/py/**"
8+
- ".github/workflows/python-ci.yml"
9+
pull_request:
10+
branches: [main, master]
11+
paths:
12+
- "packages/py/**"
13+
- ".github/workflows/python-ci.yml"
14+
15+
defaults:
16+
run:
17+
working-directory: packages/py
18+
19+
jobs:
20+
test:
21+
runs-on: ubuntu-latest
22+
strategy:
23+
matrix:
24+
python-version: ["3.9", "3.10", "3.11", "3.12", "3.13"]
25+
26+
steps:
27+
- uses: actions/checkout@v4
28+
29+
- name: Install uv
30+
uses: astral-sh/setup-uv@v4
31+
with:
32+
version: "latest"
33+
34+
- name: Set up Python ${{ matrix.python-version }}
35+
run: uv python install ${{ matrix.python-version }}
36+
37+
- name: Install dependencies
38+
run: uv sync --all-packages
39+
40+
- name: Run linting
41+
run: uv run ruff check .
42+
43+
- name: Run formatting check
44+
run: uv run ruff format --check .
45+
46+
- name: Run type checking
47+
run: uv run mypy sdk/src/context7
48+
49+
- name: Run tests
50+
env:
51+
CONTEXT7_API_KEY: ${{ secrets.CONTEXT7_API_KEY }}
52+
run: uv run pytest sdk/tests -v --cov=context7 --cov-report=xml
53+
54+
build:
55+
runs-on: ubuntu-latest
56+
needs: test
57+
58+
steps:
59+
- uses: actions/checkout@v4
60+
61+
- name: Install uv
62+
uses: astral-sh/setup-uv@v4
63+
64+
- name: Build package
65+
working-directory: packages/py/sdk
66+
run: uv build
67+
68+
- name: Upload build artifacts
69+
uses: actions/upload-artifact@v4
70+
with:
71+
name: python-sdk-dist
72+
path: packages/py/sdk/dist/
73+
74+
publish:
75+
runs-on: ubuntu-latest
76+
needs: [test, build]
77+
if: github.event_name == 'push' && github.ref == 'refs/heads/master'
78+
environment: pypi
79+
80+
steps:
81+
- uses: actions/checkout@v4
82+
83+
- name: Install uv
84+
uses: astral-sh/setup-uv@v4
85+
86+
- name: Download build artifacts
87+
uses: actions/download-artifact@v4
88+
with:
89+
name: python-sdk-dist
90+
path: packages/py/sdk/dist/
91+
92+
- name: Publish to PyPI
93+
working-directory: packages/py/sdk
94+
env:
95+
UV_PUBLISH_TOKEN: ${{ secrets.PYPI_TOKEN }}
96+
run: uv publish

packages/py/.python-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3.11

packages/py/pyproject.toml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
[project]
2+
name = "context7-workspace"
3+
version = "0.0.0"
4+
description = "Context7 Python packages workspace"
5+
requires-python = ">=3.9"
6+
license = "MIT"
7+
8+
[tool.uv]
9+
package = false
10+
11+
[tool.uv.workspace]
12+
members = ["sdk"]
13+
14+
[tool.ruff]
15+
line-length = 100
16+
target-version = "py39"
17+
18+
[tool.ruff.lint]
19+
select = ["E", "F", "I", "UP", "B", "SIM", "TCH"]
20+
ignore = ["E501"]
21+
22+
[tool.ruff.format]
23+
quote-style = "double"
24+
25+
[tool.pytest.ini_options]
26+
testpaths = ["sdk/tests"]
27+
asyncio_mode = "auto"
28+
asyncio_default_fixture_loop_scope = "function"
29+
30+
[tool.mypy]
31+
python_version = "3.9"
32+
strict = true
33+
warn_return_any = true
34+
warn_unused_ignores = true
35+
36+
[tool.pyright]
37+
pythonVersion = "3.9"
38+
typeCheckingMode = "strict"
39+
40+
[dependency-groups]
41+
dev = [
42+
"mypy>=1.19.0",
43+
"pytest>=8.4.2",
44+
"pytest-asyncio>=1.2.0",
45+
"pytest-cov>=7.0.0",
46+
"python-dotenv>=1.2.1",
47+
"ruff>=0.14.8",
48+
]

packages/py/sdk/README.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Context7 Python SDK
2+
3+
Python SDK for Context7 - Documentation retrieval for AI agents.
4+
5+
## Installation
6+
7+
```bash
8+
pip install context7
9+
```
10+
11+
Or with uv:
12+
13+
```bash
14+
uv add context7
15+
```
16+
17+
## Quick Start
18+
19+
```python
20+
import asyncio
21+
from context7 import Context7
22+
23+
async def main():
24+
# Initialize with API key (or set CONTEXT7_API_KEY env var)
25+
async with Context7(api_key="ctx7sk_...") as client:
26+
# Search for libraries
27+
results = await client.search_library("react")
28+
for lib in results.results:
29+
print(f"{lib.id}: {lib.title}")
30+
31+
# Get documentation
32+
docs = await client.get_docs("/facebook/react")
33+
for snippet in docs.snippets:
34+
print(f"{snippet.code_title}: {snippet.code_description}")
35+
36+
asyncio.run(main())
37+
```
38+
39+
## API Reference
40+
41+
### `Context7(api_key=None, base_url=None)`
42+
43+
Initialize the Context7 client.
44+
45+
- `api_key`: API key for authentication. Falls back to `CONTEXT7_API_KEY` environment variable.
46+
- `base_url`: Optional custom base URL for the API.
47+
48+
### `await client.search_library(query: str) -> SearchLibraryResponse`
49+
50+
Search for libraries by name or description.
51+
52+
### `await client.get_docs(library_id: str, **options) -> DocsResponse`
53+
54+
Get documentation for a library.
55+
56+
**Parameters:**
57+
- `library_id`: Library identifier in format `/owner/repo` (e.g., `/facebook/react`)
58+
- `version`: Optional library version (e.g., `"18.0.0"`)
59+
- `page`: Page number for pagination
60+
- `topic`: Filter docs by topic
61+
- `limit`: Number of results per page
62+
- `mode`: Type of documentation - `"code"` (default) or `"info"`
63+
- `format`: Response format - `"json"` (default) or `"txt"`
64+
65+
## License
66+
67+
MIT

packages/py/sdk/pyproject.toml

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
[project]
2+
name = "context7"
3+
version = "0.1.0"
4+
description = "Python SDK for Context7 - Documentation retrieval for AI agents"
5+
readme = "README.md"
6+
requires-python = ">=3.9"
7+
license = "MIT"
8+
authors = [
9+
{ name = "Upstash", email = "[email protected]" }
10+
]
11+
keywords = ["context7", "sdk", "documentation", "ai", "llm", "mcp", "upstash"]
12+
classifiers = [
13+
"Development Status :: 4 - Beta",
14+
"Intended Audience :: Developers",
15+
"License :: OSI Approved :: MIT License",
16+
"Programming Language :: Python :: 3",
17+
"Programming Language :: Python :: 3.9",
18+
"Programming Language :: Python :: 3.10",
19+
"Programming Language :: Python :: 3.11",
20+
"Programming Language :: Python :: 3.12",
21+
"Programming Language :: Python :: 3.13",
22+
"Typing :: Typed",
23+
]
24+
25+
dependencies = [
26+
"httpx>=0.27.0",
27+
"pydantic>=2.0.0",
28+
]
29+
30+
[project.optional-dependencies]
31+
dev = [
32+
"pytest>=8.0.0",
33+
"pytest-asyncio>=0.24.0",
34+
"pytest-cov>=5.0.0",
35+
"mypy>=1.11.0",
36+
"ruff>=0.6.0",
37+
]
38+
39+
[project.urls]
40+
Homepage = "https://context7.com"
41+
Documentation = "https://context7.com/docs"
42+
Repository = "https://github.com/upstash/context7"
43+
Issues = "https://github.com/upstash/context7/issues"
44+
45+
[build-system]
46+
requires = ["hatchling"]
47+
build-backend = "hatchling.build"
48+
49+
[tool.hatch.build.targets.wheel]
50+
packages = ["src/context7"]
51+
52+
[tool.hatch.build.targets.sdist]
53+
include = [
54+
"/src",
55+
"/tests",
56+
]
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
"""
2+
Context7 Python SDK - Documentation retrieval for AI agents.
3+
4+
This SDK provides a simple interface to search for libraries and retrieve
5+
documentation from Context7, optimized for AI agents and LLMs.
6+
7+
Example:
8+
```python
9+
import asyncio
10+
from context7 import Context7
11+
12+
async def main():
13+
async with Context7(api_key="ctx7sk_...") as client:
14+
# Search for libraries
15+
results = await client.search_library("react")
16+
17+
# Get documentation
18+
docs = await client.get_docs("/facebook/react")
19+
20+
asyncio.run(main())
21+
```
22+
"""
23+
24+
from context7.client import Context7
25+
from context7.errors import Context7APIError, Context7Error, Context7ValidationError
26+
from context7.models import (
27+
APIResponseMetadata,
28+
AuthenticationType,
29+
CodeDocsResponse,
30+
CodeExample,
31+
CodeSnippet,
32+
DocsResponse,
33+
DocsResponseBase,
34+
GetDocsOptions,
35+
InfoDocsResponse,
36+
InfoSnippet,
37+
LibraryState,
38+
Pagination,
39+
SearchLibraryResponse,
40+
SearchResult,
41+
TextDocsResponse,
42+
)
43+
44+
__all__ = [
45+
# Client
46+
"Context7",
47+
# Errors
48+
"Context7Error",
49+
"Context7APIError",
50+
"Context7ValidationError",
51+
# Models - Search
52+
"SearchResult",
53+
"SearchLibraryResponse",
54+
"APIResponseMetadata",
55+
# Models - Docs
56+
"CodeSnippet",
57+
"CodeExample",
58+
"InfoSnippet",
59+
"Pagination",
60+
"DocsResponseBase",
61+
"CodeDocsResponse",
62+
"InfoDocsResponse",
63+
"TextDocsResponse",
64+
"DocsResponse",
65+
"GetDocsOptions",
66+
# Enums
67+
"LibraryState",
68+
"AuthenticationType",
69+
]
70+
71+
__version__ = "0.1.0"

0 commit comments

Comments
 (0)