Skip to main content

Official Python SDK for Shim — JSON repair for LLM outputs

Project description

shim-sdk

Python SDK for Shim. Intercept malformed JSON from LLMs. Repair it. Return valid data.

One dependency: httpx. Sync and async clients included.

Installation

pip install shim-sdk
# Poetry
poetry add shim-sdk
# uv
uv add shim-sdk

Quick Start

from shim import ShimClient

with ShimClient(api_key="sk_live_...") as shim:
    result = shim.repair(raw_output='{"name": "John", "age": 30')

    if result.success:
        print(result.repaired)  # {'name': 'John', 'age': 30}

Async

from shim import AsyncShimClient

async with AsyncShimClient(api_key="sk_live_...") as shim:
    result = await shim.repair(raw_output='{"name": "John", "age": 30')
    print(result.repaired)

Schema Validation

Pass a JSON Schema to enforce types. Shim coerces values and reports what it changed.

schema = {
    "type": "object",
    "properties": {
        "name": {"type": "string"},
        "age":  {"type": "number"}
    },
    "required": ["name", "age"]
}

with ShimClient(api_key="sk_live_...") as shim:
    result = shim.repair(
        raw_output='{"name": "John", "age": "30"',
        schema=schema,
        mode="strict"
    )

    print(result.repaired)             # {'name': 'John', 'age': 30}
    print(result.metadata.confidence)  # 'medium' — schema repair present

Streaming

Schema and mode are set at stream.start(). Push chunks as they arrive. Finalize when the stream ends.

from shim import ShimClient

with ShimClient(api_key="sk_live_...") as shim:
    session = shim.stream.start(
        schema={"type": "object", "properties": {"name": {"type": "string"}}},
        mode="strict"
    )

    # Push chunks as they arrive from LLM
    shim.stream.push(session.session_id, '{"name": "Jo')
    shim.stream.push(session.session_id, 'hn", "age": 30')

    # Finalize when stream ends
    result = shim.stream.finalize(session.session_id)
    print(result.repaired)  # {'name': 'John', 'age': 30}

Async streaming

from shim import AsyncShimClient

async with AsyncShimClient(api_key="sk_live_...") as shim:
    session = await shim.stream.start()

    await shim.stream.push(session.session_id, '{"name": "Jo')
    await shim.stream.push(session.session_id, 'hn", "age": 30')

    result = await shim.stream.finalize(session.session_id)
    print(result.repaired)  # {'name': 'John', 'age': 30}

Error Handling

The SDK raises httpx.HTTPStatusError for transport-level failures. Shim itself always returns HTTP 200 — application errors are in the response body.

import httpx
from shim import ShimClient

with ShimClient(api_key="sk_live_...") as shim:
    try:
        result = shim.repair(raw_output="not json at all")

        if not result.success:
            for error in result.metadata.errors:
                print(f"{error.code}: {error.message}")
                print(f"  recoverable: {error.recoverable}")
    except httpx.HTTPStatusError as e:
        print(f"Transport error: {e.response.status_code}")

Types

Full type definitions included. All response objects are dataclasses with autocomplete.

Type Description
RepairResponse Top-level batch / finalize response
RepairMetadata Confidence, repairs, warnings, errors
RepairDetail Single repair operation (type, confidence, field)
Warning Non-critical issue
RepairError Critical failure with recoverability flag
StreamSession Session ID + expiry from stream.start()
StreamingState Incremental parse state from stream.push()

Support

License

MIT

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

shim_sdk-0.1.0.tar.gz (7.5 kB view details)

Uploaded Source

Built Distribution

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

shim_sdk-0.1.0-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

Details for the file shim_sdk-0.1.0.tar.gz.

File metadata

  • Download URL: shim_sdk-0.1.0.tar.gz
  • Upload date:
  • Size: 7.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.1

File hashes

Hashes for shim_sdk-0.1.0.tar.gz
Algorithm Hash digest
SHA256 afa13d3d64968e27ab146867146d30f4100215e7701c5631454819d4573ce263
MD5 dbc65aaee6673d18a055bf868722c172
BLAKE2b-256 7078d94336126ce4534684837d230e30f0614182417ef0097e7fa76e2136249e

See more details on using hashes here.

File details

Details for the file shim_sdk-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: shim_sdk-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.1

File hashes

Hashes for shim_sdk-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d7cb6131ce622cb16db7e1a46430b38d8c41e291b5561bd6364516fd0071ec7f
MD5 24763f7a0c9562328333217f72ec06aa
BLAKE2b-256 659637261976180e7c62b1727fa76c2261126aedcfd74cf66753e5b4c8e3e38b

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