ExTypesafe.Response (ExTypesafe v0.2.0)

Copy Markdown View Source

Structs representing a successful TypeSafe API response.

A Response contains:

  • model — the model that performed the evaluation
  • answers — every answer keyed exactly as its question was supplied
  • nouls, choices, and scores — typed convenience maps for each answer kind
  • usage — token counts for the request
  • request_id — the TypeSafe request ID, when the API sent one

When questions are keyed by atoms, atom keys are restored after decoding JSON, so dot access is ergonomic and does not create atoms from API-controlled data:

response.answers.is_urgent.noul
response.choices.department.choice
response.scores.frustration.score

Summary

Types

A question key preserved from the caller's request.

t()

Functions

Parses a raw API response map (as decoded JSON) into a Response struct.

Parses a raw API response and restores keys from the supplied question map or struct.

Types

answer()

answer_key()

@type answer_key() :: String.t() | atom()

A question key preserved from the caller's request.

t()

@type t() :: %ExTypesafe.Response{
  answers: %{optional(answer_key()) => answer()},
  choices: %{optional(answer_key()) => ExTypesafe.Response.ChoiceAnswer.t()},
  model: String.t() | nil,
  nouls: %{optional(answer_key()) => ExTypesafe.Response.NoulAnswer.t()},
  request_id: String.t() | nil,
  scores: %{optional(answer_key()) => ExTypesafe.Response.ScoreAnswer.t()},
  usage: ExTypesafe.Response.Usage.t() | nil
}

Functions

from_map(map)

@spec from_map(map()) :: t()

Parses a raw API response map (as decoded JSON) into a Response struct.

Since no original question map is available, answer keys remain the string IDs returned by the API. Use from_map/2 when atom keys should be restored.

from_map(map, questions)

@spec from_map(map(), term()) :: t()

Parses a raw API response and restores keys from the supplied question map or struct.

Struct containers are normalized without their __struct__ field and omit nil fields. Atom question keys are reused rather than created from strings. String question keys remain strings. The caller is responsible for ensuring that no atom and string key serialize to the same API key; ExTypesafe.Client.evaluate/4 validates that invariant before making a request.