Welcome to the new Golem Cloud Docs! 👋
Type Mapping

Type mapping

The types used in the component's code are mapped to JSON when used in the invocation REST API. This page lists all the supported types and how they are mapped.

The examples below show the JSON value part used by the REST invocation API. When calling the raw invoke endpoints, each argument and result is wrapped as a typed value object as described in the Invoke via HTTP page.

Language types to JSON

The following TypeScript types are supported:

TypeScript typeJSON representationRemarks
string"hello world"JSON string
booleantrue, falseJSON boolean
number1234.0JSON number
bigint1234JSON number
Array<T>["one", "two", "three"]JSON array
Type literal { ... }{ "a": "hello", "b": 1234 }JSON object
Interface with value fields{ "field1": "value", "field2": 42 }JSON object
Map<K, V>[["key1", 100], ["key2", -2]]Array of key-value pairs, not a JSON object
{ tag: "x", val: T1 } | { tag: "y", val: T2 } | ...{ "x": "hello" }Single-key JSON object; the val is optional, if missing the value is null
"x" | "y" | ..."x"JSON string; TypeScript enums are not supported, use string literal unions instead
Other union{ "case1": "hello" }Anonymous unions use case1, case2, etc. Named union types use the type name as a prefix, for example type UnionType = string | number becomes UnionType1, UnionType2
[T1, T2, T3]["hello", 1234, true]Fixed-order JSON array
T | undefined"value" or nullnull represents the missing value
T | null"value" or nullnull represents the missing value
field?: T42 or nullOptional fields use null for the missing case
Uint8Array[104, 101, 108, 108, 111]JSON array of numbers
Int8Array[-1, 0, 1]JSON array of numbers
Uint16Array[65535, 0, 1]JSON array of numbers
Int16Array[-32768, 0, 32767]JSON array of numbers
Uint32Array[4294967295, 0, 1]JSON array of numbers
Int32Array[-2147483648, 0, 2147483647]JSON array of numbers
BigUint64Array[18446744073709551615, 0, 1]JSON array of numbers
BigInt64Array[-9223372036854775808, 0, 9223372036854775807]JSON array of numbers
Float32Array[3.4028235e+38, 0, -3.4028235e+38]JSON array of numbers
Float64Array[1.7976931348623157e+308, 0, -1.7976931348623157e+308]JSON array of numbers

There are some special, Golem agent specific types that can be used in the agent constructor and methods as well:

Unstructured text

Unstructured text is an arbitrary string, optionally annotated with a language code, that is either passed directly to/from the agent, or as an URL reference. The agent can optionally specify the set of supported language codes.

import { UnstructuredText } from '@golemcloud/golem-ts-sdk';
 
async function exampleMethod(
    unstructuredTextWithLanguageCode: UnstructuredText<["en"]>,
): Promise<void> {
    // ...
}

The type parameter is optional; if missing, there will be no restrictions for the language of the text. It's possible to list multiple language codes that are accepted by the agent.

The type itself represents either an inline string or a remote reference:

type LanguageCode = string;
 
export type UnstructuredText<LC extends LanguageCode[] = []> =
  | {
      tag: 'url';
      val: string;
    }
  | {
      tag: 'inline';
      val: string;
      languageCode?: LC[number];
    };

Unstructured binary

Unstructured binary data is similar to unstructured text, but it is annotated (and restricted by) MIME types.

import {
  UnstructuredBinary,
} from '@golemcloud/golem-ts-sdk';
 
async exampleMethod(
  unstructuredBinaryWithMimeType: UnstructuredBinary<['application/json']>,
): Promise<void> {
    // ...
}

The type parameter specifies the set of allowed MIME types for the binary data. The type itself represents either an inline byte array or a remote reference:

type MimeType = string;
 
export type UnstructuredBinary<MT extends MimeType[] | MimeType = MimeType> =
  | {
      tag: 'url';
      val: string;
    }
  | {
      tag: 'inline';
      val: Uint8Array;
      mimeType: MT extends MimeType[] ? MT[number] : MimeType;
    };

Multimodal values

Agents can work with multimodal values as input or output. It allows passing an arbitrary number of values of different types in a single parameter or return value. When defining a parameter or return value as multimodal, we define the possible data types that can be used in that position, and the actual values will be an arbitrary number of values of these types, in any combination.

There are three variants of multimodal values supported:

  • Multimodal type , allowing to pass an arbitrary number of values which is either an unstructured text or binary.
  • MultimodalCustom<T>, allowing to pass an arbitrary number of values which is either an unstructured text or binary or a user defined structured type T.
  • MultimodalAdvanced<T> , allowing to pass an arbitrary number of values of the user defined structured type T.
 
type MyStructuredData = {
  field1: string;
  field2: number;
}
 
type TextBinaryOrCustom =
  | { tag: 'text'; val: UnstructuredText }
  | { tag: 'binary'; val: UnstructuredBinary }
  | { tag: 'custom'; val: MyStructuredData }
 
async exampleMultimodalMethod(
  input: Multimodal,
): Promise<void> {
  // ...
}
 
async exampleMultimodalCustomMethod(
  input: MultimodalAdvanced<TextBinaryOrCustom>,
): Promise<void> {
  // ...
}
 
async exampleMultimodalAdvanced(
  input: MultimodalAdvanced<TextOrImage>,
): Promise<void> {
  // ...
}
 
export type TextOrImage =
  | { tag: 'text'; val: string }
  | { tag: 'image'; val: Uint8Array }
 

For code-first agent methods, spelling the custom case out as a tagged union keeps the schema generator able to resolve the custom payload type. It is equivalent to MultimodalCustom<MyStructuredData> shown below.

Here is the definition of all three variants of multimodal in typescript SDK:

export type MultimodalAdvanced<T> = T[];
 
export type Multimodal = MultimodalAdvanced<BasicModality>;
 
export type MultimodalCustom<T> = MultimodalAdvanced<CustomModality<T>>;
 
export type BasicModality =
  | { tag: 'text'; val: UnstructuredText }
  | { tag: 'binary'; val: UnstructuredBinary }
 
export type CustomModality<T> =
  | { tag: 'text'; val: UnstructuredText }
  | { tag: 'binary'; val: UnstructuredBinary }
  | { tag: 'custom'; val: T }
 

Composite types in JSON

The REST API encodes structured values using the following JSON shapes:

Records

Records are JSON objects with the same field names as defined in the source code:

{ "a": "hello", "b": 1234 }

Variants

Variants (tagged unions with payloads) are encoded as a single-key JSON object, where the key is the case name. Cases without a payload use null as the value:

{ "restricted": ["one", "two", "three"] }
{ "none": null }
{ "any": null }

Enums

Enums (variants where all cases have no payload) are encoded as a JSON string:

"low"
"medium"
"high"

Options

Optional values are encoded as the inner value when present, or null when absent:

"hello"
null

Results

Result types are encoded as a JSON object with either an ok or an err field:

{ "ok": "success value" }
{ "err": "error message" }

If the success or error type is unit, use null:

{ "ok": null }

Tuples

Tuples are encoded as fixed-order JSON arrays:

[1234, "hello world", 103]

Lists

Lists are encoded as JSON arrays:

["one", "two", "three"]

Maps

Maps are encoded as arrays of key-value pairs (not as JSON objects, to support non-string keys):

[["key1", 100], ["key2", -2]]

Flags

Flags represent a set of selected values, encoded as a JSON array of strings:

["get", "put"]

Handles

Handles identify resources and are returned by invoking resource constructors through the Golem invocation API. They are represented as opaque strings in JSON:

"urn:worker:component-id/worker-name/resource-id"

Handles must not be constructed by hand — use the values returned from resource constructors.