JuliusBrussee/caveman · error · Error

optional profile credentials cannot be array elements

Error message

optional profile credentials cannot be array elements

What it means

When rendering optional profile credentials, arrays are expanded element-by-element: each item is rendered and must itself resolve to a defined value. If any element renders to undefined (i.e. an optional credential was absent), the renderer refuses the whole array rather than embedding a null/undefined hole in the credentials structure.

Solutions

  1. Move optional credentials out of arrays — make the whole array optional or provide a concrete value for every element
  2. Set the env var the array element's template references so it renders to a defined value
  3. Use a default/fallback in the template (e.g. ${VAR:-default}) so the element never renders undefined
  4. Replace the array with an object keyed structure where each optional field can be omitted independently
  5. If the element is genuinely optional, drop empty elements before rendering instead of relying on the renderer

Example fix

// before
{ "headers": ["Bearer ${OPTIONAL_TOKEN}"] }  // OPTIONAL_TOKEN unset
// after
{ "header": "Bearer ${OPTIONAL_TOKEN:-none}" }  // or provide a concrete value
Defensive patterns

Strategy: validation

Validate before calling

function assertNoOptionalInArray(profile: Record<string, unknown>, env: NodeJS.ProcessEnv) {
  for (const [k, v] of Object.entries(profile)) {
    if (Array.isArray(v)) {
      for (const item of v) {
        const m = typeof item === 'string' ? item.match(/\$\{([A-Z0-9_]+)\}/) : null;
        if (m && !(m[1] in env)) throw new Error(`${k} array element needs env ${m[1]}`);
      }
    }
  }
}

Try / catch

try {
  cli.renderProfile(profile);
} catch (err) {
  if ((err as Error).message === 'optional profile credentials cannot be array elements') {
    console.error('An array element rendered to undefined — set its env var or move it out of the array.');
  } else throw err;
}

Prevention

When it happens

Trigger: renderDeep encounters an Array value in the profile credentials tree, and at least one element renders to undefined — typically an element that is itself an optional/env-backed credential template whose referenced env var or gateway value is missing, so it legitimately resolves to undefined but sits inside an array where omission is not allowed.

Common situations: An env-templated credential like "${MY_TOKEN}" placed inside an array in the profile config while MY_TOKEN is unset; copying a profile template that uses optional values in list position; refactor moving a scalar optional credential into an array (e.g. header lists) without noticing optionality breaks arrays.

Understand the failure class

Background: "Invalid value" and "allowed values are" config errors: what your library rejected and how to fix it — this error's family across 41 libraries.

Related errors


AI-assisted analysis of JuliusBrussee/caveman@3ee70a1026 (2026-09-20). Data as JSON: /api/errors/f04107df767e6eac. Report an issue: GitHub.

Appendix: source

Thrown at packages/cli/src/index.ts:5819

// renderDeep applies renderTemplate to every string leaf of a JSON value — used to
// render an agent's inline-config template before it is stringified into an env var.
// Optional credential references disappear as whole object properties when their
// source variable is unavailable. Secrets never enter generated JSON: the retained
// value is the agent-native `$OPENAI_API_KEY` reference, not its expansion.
function renderDeep(v: unknown, gw = gatewayURL(), env: NodeJS.ProcessEnv = process.env, options: RenderDeepOptions = {}): unknown {
  if (v === OPTIONAL_OPENAI_KEY_ENV_TEMPLATE) {
    const key = env.OPENAI_API_KEY;
    const inherited = typeof key === "string" && !!key.trim() && !/[\r\n]/.test(key);
    return (options.optionalOpenAIKeyEnvAvailable ?? inherited)
      ? options.optionalOpenAIKeyReference ?? "$OPENAI_API_KEY"
      : undefined;
  }
  if (typeof v === "string") return renderTemplate(v, gw);
  if (Array.isArray(v)) {
    return v.map((item) => {
      const rendered = renderDeep(item, gw, env, options);
      if (rendered === undefined) throw new Error("optional profile credentials cannot be array elements");
      return rendered;
    });
  }
  if (v && typeof v === "object") {
    const out: Record<string, unknown> = {};
    for (const [k, val] of Object.entries(v as Record<string, unknown>)) {
      const rendered = renderDeep(val, gw, env, options);
      if (rendered !== undefined) out[k] = rendered;
    }
    return out;
  }
  return v;
}

function stripJson5Comments(s: string): string {
  let out = "";
  let inString = false;
  let escaped = false;

View on GitHub (pinned to 3ee70a1026)