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.1.tar.gz (7.6 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.1-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: shim_sdk-0.1.1.tar.gz
  • Upload date:
  • Size: 7.6 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.1.tar.gz
Algorithm Hash digest
SHA256 264b36954d8b8c2e4298832e11b1e80b94bd6ea8507328c7e8efc8baf7fff489
MD5 16e3a0b2c2b077d8ef796ae6d894b2bb
BLAKE2b-256 28721853299bafeb3ed36e17732301df2a71423e051157d9d9d6e1cc3c503ee3

See more details on using hashes here.

File details

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

File metadata

  • Download URL: shim_sdk-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 6.5 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 85dff71cd76896aed9cee1c9795823855ade22ea469ef5adcf9e876d12f2765e
MD5 143e6317533b9468103fd1bf7a951e12
BLAKE2b-256 d6f3e2fd02f61f961c8b95ac091ad96f27b35e044992704df880074f57b51ebe

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