Skip to main content

SilkLoom Core: minimal stateful batch engine for LLM and VLM workloads

Project description

SilkLoom Core

SilkLoom Core is a small batch engine for OpenAI-compatible LLM and VLM calls. It focuses on the parts that become painful in production scripts:

  • Jinja prompt rendering
  • text and image message assembly
  • raw text, JSON, and Pydantic output parsing
  • retry capture with raw output and reasoning extraction
  • ordered batch execution
  • completion-order streaming
  • SQLite checkpoints for resumable runs
  • sync and async APIs with the same shape

Install

pip install silkloom-core

Optional extras:

pip install "silkloom-core[data]"      # pandas export
pip install "silkloom-core[progress]"  # tqdm, if you build your own progress UI

The API

from silkloom_core import Loom

loom = Loom(
    model="gpt-4o-mini",
    prompt="Summarize this text in one sentence: {{ text }}",
    temperature=0.2,
)

result = loom.run({"text": "SilkLoom is a batch engine for model workloads."})
print(result.value)

Loom is the only high-level object. It has six execution methods:

  • run(data) runs one item.
  • arun(data) runs one item asynchronously.
  • batch(items, name=None, concurrency=5) returns a Batch in input order.
  • abatch(items, name=None, concurrency=5) is the async batch API.
  • each(items, name=None, concurrency=5, ordered=False) yields Result objects.
  • aeach(items, name=None, concurrency=5, ordered=False) is the async streaming API.

Input can be a string or a dictionary. Strings are normalized to {"text": value}.

Structured Output

Use output=dict for JSON objects:

from silkloom_core import Loom

loom = Loom(
    model="gpt-4o-mini",
    prompt="Return JSON with keys sentiment and keywords for: {{ text }}",
    output=dict,
)

result = loom.run("The method is useful, but the experiments are weak.")
print(result.unwrap()["keywords"])

Use a Pydantic model for validated typed output:

from pydantic import BaseModel
from silkloom_core import Loom

class Review(BaseModel):
    sentiment: str
    keywords: list[str]

loom = Loom(
    model="gpt-4o-mini",
    prompt="Return JSON matching the schema for: {{ text }}",
    output=Review,
)

review = loom.run({"text": "Clear writing, limited evaluation."}).unwrap()
print(review.sentiment)

SilkLoom extracts fenced JSON, can repair malformed JSON with json-repair, and separates DeepSeek-style <think>...</think> reasoning into Result.reasoning.

Checkpointed Batch Runs

Pass name to enable SQLite checkpointing. The cache key includes the input, model, prompt, system message, output schema, and model parameters.

from silkloom_core import Loom

loom = Loom(
    model="gpt-4o-mini",
    prompt="Rewrite formally: {{ text }}",
    cache=".silkloom.db",
)

items = [
    "This works pretty well.",
    "The experiment needs more detail.",
]

batch = loom.batch(items, name="formal_rewrite_v1", concurrency=8)
print(batch.values())

# A later run with the same namespace and configuration reuses successful results.
again = loom.batch(items, name="formal_rewrite_v1", concurrency=8)
print([item.cache_hit for item in again])

Streaming

Use completion-order streaming for responsive UIs:

for result in loom.each(items, name="formal_rewrite_v1", concurrency=8):
    print(result.ok, result.value)

Use ordered=True when consumers require input order:

for result in loom.each(items, concurrency=8, ordered=True):
    print(result.value)

Async streaming has the same shape:

async for result in loom.aeach(items, name="formal_rewrite_v1", concurrency=20):
    print(result.value)

Images

If an input dictionary contains images, SilkLoom builds a multimodal OpenAI-compatible message. HTTP(S) and data:image/... URLs are passed through. Local files are converted to base64 data URLs.

loom = Loom(
    model="gpt-4o-mini",
    prompt="Extract the menu item names and prices. Instruction: {{ instruction }}",
    output=dict,
)

result = loom.run(
    {
        "instruction": "Return JSON only.",
        "images": ["./receipt.jpg", "https://example.com/menu.png"],
    }
)

Result Objects

Each run returns a Result:

result.ok          # bool
result.value       # parsed value, or None on failure
result.error       # traceback string on failure
result.input       # normalized input dict
result.output      # raw model text
result.reasoning   # extracted <think> content, if present
result.cache_hit   # true when loaded from SQLite
result.attempts    # number of attempts used

result.unwrap() returns value or raises if the run failed.

Batch is an iterable container with helpers:

batch.successful()
batch.failed()
batch.values()
batch.to_dicts()
batch.to_pandas()

Custom Clients

By default, Loom creates OpenAI SDK clients. You can also pass an existing OpenAI-compatible client:

from openai import OpenAI
from silkloom_core import Loom

client = OpenAI(base_url="https://api.example.com/v1", api_key="...")

loom = Loom(
    model="provider-model",
    prompt="{{ text }}",
    client=client,
)

For full control, pass an object that implements:

class MyClient:
    def complete(self, *, model, messages, params) -> str: ...
    async def acomplete(self, *, model, messages, params) -> str: ...
    def close(self) -> None: ...
    async def aclose(self) -> None: ...

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

silkloom_core-4.0.0.tar.gz (14.0 kB view details)

Uploaded Source

Built Distribution

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

silkloom_core-4.0.0-py3-none-any.whl (13.1 kB view details)

Uploaded Python 3

File details

Details for the file silkloom_core-4.0.0.tar.gz.

File metadata

  • Download URL: silkloom_core-4.0.0.tar.gz
  • Upload date:
  • Size: 14.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for silkloom_core-4.0.0.tar.gz
Algorithm Hash digest
SHA256 d1e736504c1821408cb86e2b61a69c7d3fb58faa74d7f9698ab515d391774c92
MD5 525a5a1e9e2ba3fd6142dfd39d5d1696
BLAKE2b-256 0c7bd47a4d71922647baee199595e53b6b00e65e5a20e34ce320bfa7c55b0544

See more details on using hashes here.

Provenance

The following attestation bundles were made for silkloom_core-4.0.0.tar.gz:

Publisher: publish.yml on LeLiu-GeoAI/silkloom-core

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file silkloom_core-4.0.0-py3-none-any.whl.

File metadata

  • Download URL: silkloom_core-4.0.0-py3-none-any.whl
  • Upload date:
  • Size: 13.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for silkloom_core-4.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 aa89c563b941f989a282b03a98bbe844b5a2f095864cd96b129ca54bc166e581
MD5 8176ff4a5d36ca9d1b32470a0ec7117f
BLAKE2b-256 cf82b342150ae4f9a6d3d2d6e726e66b858e641bfee56ec144489eb69533c824

See more details on using hashes here.

Provenance

The following attestation bundles were made for silkloom_core-4.0.0-py3-none-any.whl:

Publisher: publish.yml on LeLiu-GeoAI/silkloom-core

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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