pig_protocol

Shared, provider-neutral message types and OpenAI-compatible codecs for the pig agent ecosystem.

pig_protocol provides the protocol boundary used by pig and pig_proxy:

Most codecs and parsers are pure. Network access is isolated behind transport functions, with a gleam_httpc implementation available when needed.

Installation

Add the package to a Gleam project:

gleam add pig_protocol

Usage

Build a Chat Completions request and parse its response:

import pig_protocol/codec/chat
import pig_protocol/message

pub fn request_body() -> String {
  chat.build_request_body(
    messages: [
      message.System("You are a helpful assistant."),
      message.User("What is 7 plus 3?"),
    ],
    tools: [],
    model: "gpt-4o-mini",
  )
}

pub fn parse_response(body: String) {
  chat.parse_response(body)
}

The Responses API has the same pure request/response shape through pig_protocol/codec/responses.

For direct HTTP calls, construct a pig_protocol/transport.HttpRequest and use pig_protocol/transport/httpc.transport, or provide your own function matching pig_protocol/transport.Transport.

Modules

ModulePurpose
pig_protocol/messageUnified conversation messages and tool calls
pig_protocol/inferenceProvider response values and metadata
pig_protocol/errorNormalized provider errors
pig_protocol/stop_reasonProvider-neutral completion reasons
pig_protocol/tool_definitionJSON Schema-backed tool definitions
pig_protocol/codec/chatChat Completions request and response codecs
pig_protocol/codec/responsesResponses API and Codex codecs
pig_protocol/sseIncremental SSE frame and event parsing
pig_protocol/authStandard API and Codex OAuth endpoint helpers
pig_protocol/transportHTTP transport interfaces
pig_protocol/transport/httpcgleam_httpc transport implementation

Development

From the repository root:

cd packages/pig_protocol
gleam test
gleam build --warnings-as-errors

Live integration tests are disabled by default. Run them through the repository’s integration task with the required provider credentials configured:

mise run test-integration-protocol

License

Apache-2.0

Search Document