json-function

command
v2.14.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 23, 2026 License: Apache-2.0 Imports: 6 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

JSON Function Example

Demonstrates the function-style PromptKit pattern: invoke a prompt like a serverless function — structured JSON in, one-shot, JSON out.

What it shows

  • WithJSONInput — bind a structured input to the prompt's template variables for a single Send. Top-level fields become {{topic}} / {{audience}}; the whole payload is available as {{input}}.
  • WithResponseFormat — request schema-enforced JSON output (works across Claude, OpenAI, and Gemini).
  • Empty-message one-shot — passing "" as the message makes the input JSON also fill the user turn, so a single Send is a complete invocation.
  • Typed round-trip — unmarshal the JSON response into a Go struct.

The recipe

conv, _ := sdk.Open("./research.pack.json", "plan",
    sdk.WithResponseFormat(&providers.ResponseFormat{
        Type:       providers.ResponseFormatJSONSchema,
        JSONSchema: outputSchema,
        SchemaName: "research_plan",
        Strict:     true,
    }),
)
defer conv.Close()

resp, _ := conv.Send(ctx, "", sdk.WithJSONInput(PlanRequest{
    Topic:    "utility-scale battery storage growth in 2023",
    Audience: "executive",
}))

var plan PlanResponse
json.Unmarshal([]byte(resp.Text()), &plan)

Binding rules

For a JSON object input:

  • {{input}} is bound to the whole object (compact JSON).
  • Each top-level field is bound to {{field}} — strings pass through verbatim; numbers, booleans, objects, and arrays are bound as their compact JSON.
  • Bound variables override open-time WithVariables defaults.
  • A real top-level field named input shadows the synthetic whole-object var.

Run it

export OPENAI_API_KEY=your-key   # or ANTHROPIC_API_KEY / GEMINI_API_KEY
go run .

The smoke test (go test ./...) exercises the same path with a mock provider — no API keys required.

Note: prefer WithResponseFormat with a schema over the schemaless WithJSONMode for cross-provider output — WithJSONMode has no effect on Anthropic, which only supports schema-backed structured output.

Documentation

Overview

Package main demonstrates the "function-style" PromptKit pattern: a prompt invoked like a serverless function — structured JSON in, one-shot, JSON out.

It shows:

  • WithJSONInput: binding a structured input to the prompt's {{variables}} (top-level fields become {{topic}}/{{audience}}; the whole payload is {{input}})
  • WithResponseFormat: requesting schema-enforced JSON output
  • A single Send with an empty message, so the input JSON also fills the user turn
  • Unmarshaling the JSON response into a Go struct

Run with:

export OPENAI_API_KEY=your-key
go run .

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL