Skip to main content

A unified LLM response parser with streaming support for multiple providers

Project description

StreamShape

A Python package that provides a simple, consistent interface for interacting with multiple Large Language Model (LLM) providers. Write once, run anywhere - switch between OpenAI, Anthropic, Google, and other providers without changing your code.

Key Features

5 Output Modes

  • Normal text generation
  • Streaming text
  • Function calling (tool use)
  • Structured output (validated Pydantic objects)
  • Streaming structured output

🔌 6 Supported Providers

  • OpenAI (GPT-4, GPT-3.5)
  • Anthropic (Claude)
  • Google (Gemini)
  • OpenRouter (100+ models)
  • xAI (Grok)
  • Any OpenAI-compatible endpoint (Ollama, LM Studio, etc.)

🛡️ Type-Safe & Validated

  • Full type hints for IDE autocomplete
  • Pydantic validation for structured outputs
  • Clear, consistent error messages

🎯 Simple & Consistent

  • Same API across all providers
  • Built on battle-tested libraries (LiteLLM, Pydantic)
  • Minimal code to get started

Quick Start

Installation

pip install streamshape

Or install from source:

git clone https://github.com/saikethan27/StreamShape.git
cd src/streamshape
pip install -e .

Basic Usage

from streamshape import OpenAI
from pydantic import BaseModel

# Initialize provider
client = OpenAI(api_key="your-api-key")

# 1. Generate text
response = client.generate(
    model="gpt-4",
    system_prompt="You are a helpful assistant.",
    user_prompt="What is Python?"
)
print(response["data"])

# 2. Stream text
for chunk in client.stream(
    model="gpt-4",
    system_prompt="You are a storyteller.",
    user_prompt="Tell me a short story"
):
    print(chunk["data"], end="", flush=True)

# 3. Structured output
class Recipe(BaseModel):
    name: str
    ingredients: list[str]
    steps: list[str]

result = client.structured_output(
    model="gpt-4",
    system_prompt="You are a chef.",
    user_prompt="Give me 2 simple pasta recipes",
    output_schema=Recipe
)

for recipe in result["data"]:
    print(f"Recipe: {recipe.name}")
    print(f"Ingredients: {', '.join(recipe.ingredients)}")

Switch Providers Easily

# Just change the import - same API!

from streamshape import OpenAI
client = OpenAI(api_key="sk-...")

# Or use Anthropic
from streamshape import Anthropic
client = Anthropic(api_key="sk-ant-...")

# Or Google
from streamshape import Google
client = Google(api_key="...")

# Same code works for all providers!

Documentation

📚 Complete documentation available in the /docs folder:

Five Ways to Use LLMs

1. Generate (Complete Text)

from streamshape import OpenAI

client = OpenAI(api_key="your-api-key")

response = client.generate(
    model="gpt-4",
    system_prompt="You are a helpful assistant.",
    user_prompt="Explain quantum computing in simple terms",
    temperature=0.7
)
print(response["data"])

2. Stream (Real-time Text)

for chunk in client.stream(
    model="gpt-4",
    system_prompt="You are a helpful assistant.",
    user_prompt="Write a short story",
    temperature=0.7
):
    print(chunk["data"], end="", flush=True)

3. Tool Call (Function Calling)

tools = [{
    "type": "function",
    "function": {
        "name": "get_weather",
        "description": "Get current weather",
        "parameters": {
            "type": "object",
            "properties": {
                "location": {"type": "string"}
            }
        }
    }
}]

result = client.tool_call(
    model="gpt-4",
    system_prompt="You are a helpful assistant.",
    user_prompt="What's the weather in Paris?",
    tools=tools
)

print(result["data"]["tool_name"])    # "get_weather"
print(result["data"]["arguments"])    # {"location": "Paris"}

4. Structured Output (Complete)

from pydantic import BaseModel

class Task(BaseModel):
    title: str
    priority: str
    estimated_hours: int

result = client.structured_output(
    model="gpt-4",
    system_prompt="You are a project manager.",
    user_prompt="Create 5 tasks for building a website",
    output_schema=Task
)

for task in result["data"]:
    print(f"Task: {task.title} (Priority: {task.priority})")

5. Structured Streaming Output

for task in client.structured_streaming_output(
    model="gpt-4",
    system_prompt="You are a project manager.",
    user_prompt="Create 10 tasks for building a mobile app",
    output_schema=Task
):
    print(f"New task: {task.title} (Priority: {task.priority})")

Real-World Examples

Check out the /example directory for complete working examples:

Testing

Run the test suite to verify everything works:

# Run all tests
python -m pytest tests/

# Run specific test files
python -m pytest tests/test_normalizer.py
python -m pytest tests/test_base_provider.py
python -m pytest tests/test_streaming_structered_output.py

Tests use mock payloads and Hypothesis for property-based testing, so no API calls are made during testing.

Environment Variables

Store your API keys securely using environment variables:

# .env file
OPENAI_API_KEY=sk-...
ANTHROPIC_API_KEY=sk-ant-...
GOOGLE_API_KEY=...
OPENROUTER_API_KEY=sk-or-...
XAI_API_KEY=...

Load them in your code:

import os
from dotenv import load_dotenv
from streamshape import OpenAI

load_dotenv()
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))

Project Structure

streamshape/
├── src/
│   └── streamshape/              # Main package source code
│       ├── base.py               # Base provider class
│       ├── providers.py          # Provider implementations
│       ├── exceptions.py         # Custom exceptions
│       ├── litellm_integration.py    # LiteLLM integration
│       ├── parser_integration.py     # Parser integration
│       └── streaming_structured_output_parser/  # Streaming parser
├── docs/                         # Complete documentation
├── tests/                        # Test suite
├── example/                      # Real-world examples
└── requirements.txt              # Dependencies

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is open source and available under the MIT License.

Support

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

streamshape-0.1.2.tar.gz (15.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

streamshape-0.1.2-py3-none-any.whl (14.9 kB view details)

Uploaded Python 3

File details

Details for the file streamshape-0.1.2.tar.gz.

File metadata

  • Download URL: streamshape-0.1.2.tar.gz
  • Upload date:
  • Size: 15.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.2

File hashes

Hashes for streamshape-0.1.2.tar.gz
Algorithm Hash digest
SHA256 c8ec8a56c7ea17a379bfcb20a8d56ba7cc147bf813e5b74a7bec982f4d136467
MD5 1c1a73d8d8d1aedc521d5264e2aaebce
BLAKE2b-256 01baa52ea11d5484bb04706fc3eef8fb5a4f21ebfba63cd14d7042c092722a5f

See more details on using hashes here.

File details

Details for the file streamshape-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: streamshape-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 14.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.2

File hashes

Hashes for streamshape-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 8a422bee441b6b74f3dfae060f662ed2ad6077f784499dc90cdd3e9bda358c2a
MD5 a915942170243fb542540980c05caa5f
BLAKE2b-256 f7749ce9b37c04843b79306d92f1d066027adee55b34fe643132cfae44f8fc50

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page