http-transforms

command
v2.22.0 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2026 License: Apache-2.0 Imports: 11 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

HTTP Transforms Example

Customize HTTP tool behavior with argument transforms, request hooks, and response processing.

What You'll Learn

  • WithTransform() to map/normalize arguments before the HTTP request
  • WithPreRequest() to inject headers, correlation IDs, or dynamic auth
  • WithPostProcess() to transform responses after receiving
  • WithRedact() to strip sensitive fields before sending to the LLM

Prerequisites

  • Go 1.26+
  • An API key for one of: OpenAI, Anthropic, or Google (the packs do not pin a provider)

Running the Example

export OPENAI_API_KEY=your-key
go run .

Code Overview

Argument Transform

Map LLM arguments to the format your API expects:

cfg := sdktools.NewHTTPToolConfig("https://api.example.com/users",
    sdktools.WithTransform(func(args map[string]any) (map[string]any, error) {
        name, _ := args["name"].(string)
        parts := strings.SplitN(name, " ", 2)
        return map[string]any{
            "first_name": parts[0],
            "last_name":  parts[1],
        }, nil
    }),
)
conv.OnTool("lookup_user", cfg.Handler())
Pre-Request Hook

Modify the HTTP request before sending (auth, tracing, etc.). The URL template puts the user_id argument in the path:

cfg := sdktools.NewHTTPToolConfig("https://api.example.com/users/{{.user_id}}/orders",
    sdktools.WithMethod("GET"),
    sdktools.WithPreRequest(func(req *http.Request) error {
        req.Header.Set("X-Correlation-ID", generateID())
        req.Header.Set("Authorization", "Bearer "+getToken())
        return nil
    }),
)
conv.OnToolCtx("get_orders", cfg.HandlerCtx())
Response Post-Processing + Redaction

Strip sensitive fields and add computed values:

cfg := sdktools.NewHTTPToolConfig("https://api.example.com/customers",
    sdktools.WithRedact("ssn", "credit_card"),
    sdktools.WithPostProcess(func(resp []byte) ([]byte, error) {
        var data map[string]any
        json.Unmarshal(resp, &data)
        data["display_name"] = data["first_name"].(string) + " " + data["last_name"].(string)
        return json.Marshal(data)
    }),
)
conv.OnTool("get_customer", cfg.Handler())

Processing Pipeline

For each tool call, the pipeline runs in order:

  1. Transform - Normalize arguments (WithTransform)
  2. Pre-Request - Modify HTTP request (WithPreRequest)
  3. Execute - Send HTTP request
  4. Post-Process - Transform response (WithPostProcess)
  5. Redact - Strip sensitive fields (WithRedact)

Key Concepts

  1. Argument Mapping - Bridge the gap between LLM output and API input
  2. Dynamic Auth - Inject tokens at request time (OAuth refresh, etc.)
  3. Field Redaction - Prevent sensitive data from reaching the LLM
  4. Computed Fields - Enrich responses before the LLM sees them

Next Steps

Documentation

Overview

Package main demonstrates HTTP tool transforms and response processing via the SDK.

This example shows three patterns for HTTP tool customization:

  • WithTransform: normalize/map LLM arguments before the request
  • WithPreRequest: inject headers, tracing IDs, or auth at request time
  • WithPostProcess + WithRedact: transform responses and strip sensitive fields

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