Last reviewed: November 21, 2026 (Asia/Shanghai); primary references fetched November 20, 2026 UTC.

Replay saved CometAPI response fixtures before making a live request. A small Haskell checker can read bounded JSON files, classify a completed text choice, and reject refusals, truncation, extra choices, or malformed envelopes. That gives a model or SDK upgrade a deterministic contract check without spending a request. Keep the fixture verdict separate from transport evidence: a parser can prove what a saved body means, but it cannot prove that a remote attempt was never accepted.

This is a response-replay workflow, rather than another language-specific API client. It is useful when a service already has a transport adapter and needs a cheap gate for model changes, decoder changes, or incident replay. The references checked here are aeson 2.3.2.0, http-client 0.7.19, and http-client-tls 0.4.0, plus the current CometAPI chat contract.

Create a fixture-check project with a real destination

Create the project directory first, then initialize Cabal inside it. The positional directory is deliberate; --package-name names the package and does not create or enter a directory.

mkdir cometapi-fixture-check
cd cometapi-fixture-check
cabal init --non-interactive --exe --language Haskell2010 --package-name cometapi-fixture-check
cabal add aeson bytestring text
mkdir -p fixtures

If cabal add is unavailable, put these bounds in the executable stanza:

build-depends:
    aeson >= 2.3 && < 2.4,
    base >= 4.14 && < 5,
    bytestring,
    text

The Aeson module reference documents FromJSON, encode, and eitherDecode. The CometAPI chat reference defines the chat.completion object, one or more choices, message roles, and finish outcomes such as stop and length. Save only sanitized, non-sensitive response bodies as fixtures. Do not put authorization headers, prompts containing private data, or account identifiers in the fixture directory.

Classify a bounded completion body

Replace app/Main.hs with this checker. It accepts a fixture path and an expected verdict, then fails closed when the file is too large or the JSON does not match the text-only contract.

{-# LANGUAGE OverloadedStrings #-}

module Main (main) where

import qualified Data.Aeson as A
import Data.Aeson ((.:), (.:?))
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as LBS
import qualified Data.Text as T
import System.Environment (getArgs)
import System.Exit (die)
import System.IO (IOMode(ReadMode), withBinaryFile)

maxFixtureBytes :: Int
maxFixtureBytes = 1024 * 1024

data Verdict = AcceptedText T.Text | Rejected T.Text
  deriving (Eq, Show)

data Completion = Completion T.Text [Choice]
data Choice = Choice (Maybe T.Text) Reply
data Reply = Reply T.Text (Maybe T.Text) (Maybe A.Value) (Maybe [A.Value])

instance A.FromJSON Completion where
  parseJSON = A.withObject "Completion" $ \o ->
    Completion <$> o .: "object" <*> o .: "choices"

instance A.FromJSON Choice where
  parseJSON = A.withObject "Choice" $ \o ->
    Choice <$> o .:? "finish_reason" <*> o .: "message"

instance A.FromJSON Reply where
  parseJSON = A.withObject "Reply" $ \o ->
    Reply <$> o .: "role"
          <*> o .:? "content"
          <*> o .:? "refusal"
          <*> o .:? "tool_calls"

classify :: LBS.ByteString -> Verdict
classify body = case A.eitherDecode body of
  Left _ -> Rejected "invalid_json"
  Right (Completion object choices) -> case choices of
    [Choice finish (Reply role content refusal tools)]
      | object == "chat.completion"
      , finish == Just "stop"
      , role == "assistant"
      , refusal == Nothing
      , maybe True null tools
      , Just text <- content
      , not (T.null (T.strip text)) -> AcceptedText text
    _ -> Rejected "unaccepted_completion"

readFixture :: FilePath -> IO (Either T.Text LBS.ByteString)
readFixture path = withBinaryFile path ReadMode $ \handle ->
  let go total chunks = do
        chunk <- BS.hGetSome handle 65536
        if BS.null chunk
          then pure (Right (LBS.fromChunks (reverse chunks)))
          else if total + BS.length chunk > maxFixtureBytes
            then pure (Left "fixture_too_large")
            else go (total + BS.length chunk) (chunk : chunks)
  in go 0 []

expected :: String -> Maybe T.Text
expected "accepted" = Just "accepted"
expected "rejected" = Just "rejected"
expected _ = Nothing

main :: IO ()
main = do
  args <- getArgs
  case args of
    [path, want] -> case expected want of
      Nothing -> die "usage: fixture-check FILE accepted|rejected"
      Just wanted -> do
        loaded <- readFixture path
        case loaded of
          Left errorCode -> die (T.unpack errorCode)
          Right body -> case (wanted, classify body) of
            ("accepted", AcceptedText text) -> putStrLn (T.unpack text)
            ("rejected", Rejected _) -> putStrLn "rejected as expected"
            _ -> die "fixture verdict mismatch"
    _ -> die "usage: fixture-check FILE accepted|rejected"

readFixture applies the byte ceiling before Aeson sees the body. classify then checks the provider object, exactly one choice, the assistant role, a natural stop, no refusal, no nonempty tool calls, and nonblank text. A valid JSON document with finish_reason set to length is still rejected because this fixture contract promises complete text. The rejection label is intentionally coarse; keep raw bodies out of ordinary diagnostics and inspect a fixture privately when a test fails.

The checker treats a missing or null refusal as absent, accepts an absent or empty tool_calls array, and rejects a nonempty tool-call result. If your application supports tools or multimodal content, add a separate verdict type and fixtures rather than weakening this one. That makes a change in product behavior visible in the test review.

Add fixtures that represent decisions

Create a small set of sanitized files. The accepted case can be:

{"object":"chat.completion","choices":[{"finish_reason":"stop","message":{"role":"assistant","content":"fixture answer."}}]}

Keep separate rejected fixtures for a length finish reason, a refusal, two choices, missing text, and malformed JSON. Their filenames should describe the observed state, such as length.json or two-choices.json; do not encode an API key or user prompt in a filename. Run the checker without network access:

cabal run -- fixtures/accepted.json accepted
cabal run -- fixtures/length.json rejected
cabal run -- fixtures/two-choices.json rejected

The first command should print the fixture text. The rejected cases should print rejected as expected. Add the commands to CI and fail the job when an expected verdict changes. The test proves the decoder’s decision for that saved body; it does not establish that the live provider still emits the same fields.

For a useful drift review, retain the fixture’s source date, model identifier, and sanitized outcome beside the file. When a dependency upgrade changes FromJSON behavior or a provider adds a field, compare the semantic verdict and the raw fixture hash. A changed fixture should be reviewed as evidence, not silently overwritten by a new run.

Keep live transport retries and body deadlines explicit

A fixture run makes no network call, so it cannot replay a generation. If you add a live smoke command around the same parser, configure its manager deliberately. The http-client reference documents managerRetryableException :: ManagerSettings -> SomeException -> Bool; the default can retry some failures on reused connections. The http-client-tls module provides newTlsManagerWith, so a no-replay manager can be built explicitly:

import Network.HTTP.Client (Manager, ManagerSettings(..), newManager)
import Network.HTTP.Client.TLS (tlsManagerSettings)

noReplayTlsManager :: IO Manager
noReplayTlsManager = newManager
  (tlsManagerSettings { managerRetryableException = const False })

Use that setting only when your application wants one transport attempt per call. A retry policy belongs above the parser, where the caller can attach an operation identity and reconcile an uncertain result. Do not claim that a fixture verdict proves exactly-once execution.

responseTimeoutMicro limits retrieval of response headers; it does not automatically time out later brRead calls. If a live smoke test reads a streamed body, put System.Timeout.timeout around the bounded reader inside the withResponse callback. That keeps connection acquisition and header retrieval outside the body deadline while still letting withResponse close the response on timeout. For example, the shape is:

bodyResult <- withResponse request manager $ \response ->
  timeout bodyMicros (readBounded (responseBody response) maxBytes)

Treat Nothing as body_timeout, and keep the header timeout and body deadline as separate fields in the test result. This distinction prevents a server that sends headers and then stalls from being reported as a successful transport check.

Sources checked

These four primary public references returned HTTP 200 when fetched on November 20, 2026 UTC. The CometAPI page is the current wire contract. The three Hackage pages are version-pinned evergreen references for the Haskell APIs used by the fixture checker and its optional live-smoke seam.

The provider page supports the fields the fixtures classify. Aeson supports the parser. The http-client references support the optional live-smoke lifecycle, explicit no-replay setting, and the distinction between header and body timing. None of these sources proves that a particular account can access a model or that a remote request completed; those are separate runtime observations.

For contrast, Build a CometAPI Rust Client with reqwest covers an end-to-end client. Build a CometAPI Go Client with net/http covers another transport implementation. This guide stays focused on replaying evidence before transport is invoked.

Reader next step

Add the accepted, truncation, refusal, extra-choice, and malformed fixtures; run them with network access disabled; and review every verdict change in code review. If you later add a live smoke path, use the no-replay manager setting and a separate body deadline, record the model and fixture hash, and reconcile uncertain outcomes before any retry.