Skip to content

openai-client 0.2.3 — init(required) for record-shape models

Choose a tag to compare

@ggreif ggreif released this 30 Apr 08:00
· 6 commits to main since this release

Added

init(required : { … }) : T on every record-shape model. Spares the user from typing nulls for every optional field. Pair with the record-update syntax to set selected optionals:

// before — 30 fields, ~28 nulls:
let req : CreateChatCompletionRequest = {
  model = "gpt-4o-mini";
  messages = [...];
  max_tokens = ?512;
  metadata = null; temperature = null; top_p = null; /* ...25 more nulls... */
};

// after:
let req = { CreateChatCompletionRequest.init { model = "gpt-4o-mini"; messages = [...] }
            with max_tokens = ?512 };

Each model's user-facing type is restructured as public type T = Required and Optional (record-type intersection), with Required exposed as a public sub-type and Optional private — purely a scaffold for init's parameter type. Wire shape and all existing APIs (toCandidValue / fromCandidValue / toText) are unchanged.

Implementation uses Candid round-trip: from_candid(to_candid(required)) — Candid record subtyping fills the absent optional fields with null. Few cycles per call (init is not on a hot path), generated code stays compact regardless of how many optional fields the model has.