npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

structformatter

v0.1.3

Published

OpenAI-compatible proxy that enforces JSON Schema structured outputs for upstream LLM APIs that do not support native constrained decoding.

Downloads

240

Readme

StructFormatter

StructFormatter 是一个 OpenAI 兼容的代理服务(sidecar/proxy):让不支持原生 JSON Schema 约束解码(A) 的上游模型,也能“像支持 A 一样”返回结构化 JSON。

它用一套稳健的 B 策略闭环 来强制输出满足 schema: 提示词 → JSON 抽取 → JSON 修复(jsonrepair)→ JSON Schema 校验(Ajv)→ 机械修补 → 带错误信息 re-ask 重试。

specs/ 目录是项目的设计与验收来源(架构、API 合约、算法、测试计划、任务清单)。

适用场景

很多厂商只提供 “JSON mode”(保证输出是合法 JSON),但不保证符合你提供的 JSON Schema。

如果你的 agent 流程强依赖结构化输出,StructFormatter 可以作为一个“外置桥接服务”,让你的 agent 基本不用改逻辑,只改 base_url 即可。

快速开始(源码运行)

pnpm install
cp config.example.yaml config.yaml
export STRUCTFORMATTER_CONFIG=./config.yaml
pnpm dev

对外接口:

  • GET /healthz
  • GET /v1/models
  • POST /v1/chat/completions

快速开始(npm)

pnpm dlx structformatter --config ./config.yaml

或:

npx structformatter --config ./config.yaml

如何嵌入现有 agent(Drop-in)

把 OpenAI SDK 的 base_url 指向本服务:

  • Base URL:http://localhost:8080/v1
  • model 命名:provider/model(例如 deepseek/deepseek-chat
    • 或者在 config.yaml 里用 routing.model_aliases 做别名映射

当你的请求包含:

{
  "response_format": {
    "type": "json_schema",
    "json_schema": { "name": "X", "strict": true, "schema": { "...": "..." } }
  }
}

StructFormatter 会承诺:

  • 成功:choices[0].message.content可解析且符合 schema 的 JSON 字符串
  • 失败:HTTP 422 + 结构化错误(见 specs/api.md

配置说明

建议从 config.example.yaml 复制一份:

  • providers.<name>.base_url:上游 OpenAI-compatible 地址
  • providers.<name>.api_key_env:从环境变量读取 key(避免写死在文件里)
  • providers.<name>.capabilities.json_object:上游支持 JSON mode 时建议开启
  • routing.model_aliases:可选别名映射(如 glmzai/glm-4.5

调试与可观测性

可选请求头:

  • X-SF-Debug: 1 → 响应中包含 __debug
  • X-SF-Max-Attempts: 5 → 覆盖本次请求的最大重试次数(1–10)

Streaming(v1 规则)

严格遵循 specs/api.md

  • stream=truejson_schema400(v1 不支持)
  • stream=true 且非 json_schemaSSE 透传上游

示例

bash examples/curl_schema_enforced.sh
python examples/python_openai_sdk_dropin.py