This repository was archived by the owner on Jun 7, 2026. It is now read-only.
feat: Python SDK for Koine gateway - #42
Merged
Merged
Conversation
Closes #2 (partial) - Create packages/sdks/python/ with hatch build system - Add pyproject.toml with pinned dependencies (httpx, pydantic) - Add dev dependencies (pytest, pytest-asyncio, pytest-httpx, pyright, ruff) - Configure pyright for strict type checking - Add py.typed marker for PEP 561 compliance - Add README.md with usage examples - Add __init__.py with public API exports (placeholder imports)
- Add KoineError exception class with error codes - Add KoineConfig dataclass for gateway configuration - Add KoineUsage, GenerateTextResult, GenerateObjectResult models - Add StreamTextResult dataclass for streaming results - Add internal response types for gateway parsing - Add client.py stub with function signatures - Update .gitignore with Python patterns - Add uv.lock for reproducible dependencies
- Add generate_text() for plain text generation - Add generate_object() with Pydantic schema validation - Use httpx.AsyncClient for async HTTP requests - Convert Pydantic models to JSON Schema for gateway - Add proper error handling with KoineError - Rename internal types to Gateway* prefix (not underscore private)
- Add SSE parser async generator for parsing event streams - Add stream processor that resolves futures as events arrive - Implement stream_text() with AsyncIterator for text chunks - Use asyncio.Future for session_id (early), usage (late), text (late) - Update StreamTextResult to use proper async futures - Handle critical vs non-critical event parse errors - Properly close httpx client and response on stream completion
19 focused tests covering: - KoineError construction and properties - generate_text: success, system prompts, HTTP errors, invalid responses - generate_object: schema conversion, validation, session continuity - stream_text: SSE parsing, early session resolution, error handling
- hello.py: Basic generate_text usage - extract_recipe.py: Structured output with Pydantic schemas - stream.py: Real-time streaming with stream_text - conversation.py: Multi-turn conversations with session_id
- Omit None values from request body (gateway rejects null) - Add camelCase aliases to KoineUsage for gateway response parsing - Update test mocks to use camelCase field names
Consume usage and text futures after stream error to prevent "Future exception was never retrieved" warning.
- Add python-dotenv==1.2.1 to dev dependencies - Update all Python examples to use load_dotenv(find_dotenv()) - Examples now auto-find .env from parent directories
- Update root README with Python SDK in intro and packages table - Update docs/README with Python SDK quick start example - Update docs/sdk-guide.md with Python examples for all sections - Update docs/examples/README.md with correct Python run instructions - Restructure packages/sdks/python/README.md to match TypeScript pattern - Update CONTRIBUTING.md to mark Python SDK as completed
- Replace broad `except Exception` with specific exception types - Remove undocumented NO_RESPONSE_BODY error code from docstring - Catch ValidationError alongside JSONDecodeError in SSE processing - Remove dead TYPE_CHECKING import and empty block - Simplify session_id tracking using future.done() instead of variable - Add test_with_session_id for generate_text
Rename future attributes with underscore prefix to guide users toward the convenience methods (session_id(), usage(), text()) instead of direct future access.
Fixes resource leak by requiring stream_text to be used with async with:
async with stream_text(config, prompt="Hello") as result:
async for chunk in result.text_stream:
print(chunk)
This ensures HTTP client and response are always cleaned up, even if
the stream is not fully consumed or an exception occurs.
BREAKING CHANGE: stream_text now returns a context manager instead of
a coroutine. Must use `async with stream_text(...)` instead of
`await stream_text(...)`.
- Setup Python 3.13 and uv - Run ruff linting and pyright type checking - Run pytest with coverage reporting - Add Python coverage to Codecov upload
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
This was referenced Dec 25, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Implements a complete Python SDK for Koine gateway, closing #2.
generate_text,generate_object,stream_textFeatures
Text Generation
Streaming (with async context manager for resource safety)
Structured Output
Changes
New Files
packages/sdks/python/— Complete Python SDK packagesrc/koine_sdk/— Client, types, errors modulestests/— Comprehensive test suitepyproject.toml— Package configuration with ruff, pyright, pytestDocumentation Updates
docs/sdk-guide.md— Added Python examples alongside TypeScriptdocs/README.md— Added Python SDK quick startdocs/examples/python/— Runnable examples (hello, stream, conversation, extract_recipe)packages/sdks/python/README.md— SDK documentationCONTRIBUTING.md— Marked Python SDK as complete in roadmapREADME.md— Added Python SDK to packages tableCI Updates
.github/workflows/ci.yml— Added Python linting and testing with uv cachingTest plan
Related Issues