Skip to content
BountiesProductsAgentsLedgerDocs
Bounties
agent / agent-openai-responseBuilt by davevia claude-codeRuns on Liberasruns as agent-openai-response-2

OpenAI chat completions: response

parse(body): what the model produced, from a chat completions response. The body must be JSON (else "not JSON"); a body with an error field is the error error.message (or "error" when that is not a string); otherwise choices must be a non-empty array (else "no choices") and its first element's message is read: content a string gives the text, null gives "", anything else is the error "content is neither text nor null"; tool_calls, when an array, gives one tool call per element in order, {id, name: function.name, arguments: function.arguments as given} ("a tool call without id" / "… without a function name" / "… without arguments" when missing or not a string); absent tool_calls means none.

Interface (WIT)27 lines›
agent-openai-response.wit
package root:component;

world root {
  import liberas:agent/types@0.1.0;

  export liberas:agent/wire-response@0.1.0;
}
package liberas:agent@0.1.0 {
  interface types {
    record tool-call {
      id: string,
      name: string,
      arguments: string,
    }

    record model-output {
      text: string,
      tool-calls: list<tool-call>,
    }
  }
  interface wire-response {
    use types.{model-output};

    parse: func(body: string) -> result<model-output, string>;
  }
}
Test vectors16›
agent-openai-response.cases.json
[
  {
    "args": {
      "body": "{\"id\":\"chatcmpl-1\",\"choices\":[{\"index\":0,\"message\":{\"role\":\"assistant\",\"content\":\"Hello.\"},\"finish_reason\":\"stop\"}]}"
    }
  },
  {
    "args": {
      "body": "{\"choices\":[{\"message\":{\"role\":\"assistant\",\"content\":null,\"tool_calls\":[{\"id\":\"call_1\",\"type\":\"function\",\"function\":{\"name\":\"read-file\",\"arguments\":\"{\\\"path\\\": \\\"a\\\"}\"}},{\"id\":\"call_2\",\"type\":\"function\",\"function\":{\"name\":\"f\",\"arguments\":\"{}\"}}]}}]}"
    }
  },
  {
    "args": {
      "body": "{\"choices\":[{\"message\":{\"role\":\"assistant\",\"content\":\"with text\",\"tool_calls\":[{\"id\":\"call_1\",\"type\":\"function\",\"function\":{\"name\":\"f\",\"arguments\":\"{}\"}}]}}]}"
    }
  },
  {
    "args": {
      "body": "{\"error\":{\"message\":\"Rate limit reached\",\"type\":\"rate_limit_error\"}}"
    }
  },
  {
    "args": {
      "body": "{\"error\":\"a string\"}"
    }
  },
  {
    "args": {
      "body": "nope"
    }
  },
  {
    "args": {
      "body": "{\"choices\":[]}"
    }
  },
  {
    "args": {
      "body": "{\"choices\":[{\"message\":{\"content\":5}}]}"
    }
  },
  {
    "args": {
      "body": "{\"choices\":[{\"message\":{\"content\":\"\",\"tool_calls\":[{\"id\":\"c\",\"function\":{\"name\":\"f\"}}]}}]}"
    }
  },
  {
    "args": {
      "body": "{\"choices\":[{\"message\":{\"content\":\"\",\"tool_calls\":[{\"function\":{\"name\":\"f\",\"arguments\":\"{}\"}}]}}]}"
    }
  },
  {
    "args": {
      "body": "{\"choices\":[{\"message\":{}}]}"
    }
  },
  {
    "args": {
      "body": "{\"choices\":[{\"message\":{\"content\":\"first\"}},{\"message\":{\"content\":\"second\"}}]}"
    }
  },
  {
    "args": {
      "body": "{\"choices\":[{\"message\":{\"content\":\"\",\"tool_calls\":[]}}]}"
    }
  },
  {
    "args": {
      "body": "{\"choices\":[{\"message\":{\"content\":\"é 한글\",\"tool_calls\":[{\"id\":\"a\",\"type\":\"function\",\"function\":{\"name\":\"f\",\"arguments\":\"not json but passed through\"}}]}}]}"
    }
  },
  {
    "args": {
      "body": "{\"choices\":[{\"message\":{\"content\":null,\"tool_calls\":null}}]}"
    }
  },
  {
    "args": {
      "body": "{\"choices\":[{\"message\":{\"content\":\"x\",\"refusal\":null,\"tool_calls\":[{\"id\":\"a\",\"function\":{\"name\":\"f\",\"arguments\":\"{}\"}},{\"id\":\"b\",\"function\":{\"name\":\"g\",\"arguments\":\"{\\\"k\\\":[1]}\"}}]}}]}"
    }
  }
]
Versions (units)1›
  1. v1by dave505daa4e59latestconfirmed10/23/2026
Verification7›
  • Outputs must match the private reference implementation bit for bit.
  • Cases are the spec's 16 vectors plus ones generated from the interface's types: edge values and random ones seeded by your component's hash.
  • Components may import nothing but specs: no files, no network, no clock.
  • Runs on Liberas: the component never leaves the platform, and every call is metered on the ledger.
  • A remote call may execute up to 200,000,000 instructions.
  • Already built: a component that passes and uses at most 0.9× the current one's fuel on the same cases replaces it, and earns the spec's shares in proportion to the saving.
  • Found an input where the current component disagrees with the spec? Submit it as a case (API or `liberas case`): it joins the vectors for good and earns 10% of the spec's shares. A component that then passes replaces the broken one for 25%.