JsonPromptGenerator: nested object/array fields render as '[object Object]' in templates

Open Beginner friendly
#46 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
82/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Quiet
Tech stack
typescript
Domain
tooling

Research direction

Start in src/prompt-generators/json.ts at buildVariables and reproduce the issue with the nested reporter and tags fields shown in the body. Add coverage for nested values through promptTemplate, then verify the generated prompt contains faithful field representations instead of [object Object] or comma-joined arrays.

Written by the indexing model from the issue text.

Description

bug S3

Observed behavior

buildVariables in src/prompt-generators/json.ts coerces every top-level field of an object element to a string with a bare String(val):

for (const [key, val] of Object.entries(
  element as Record<string, unknown>,
)) {
  variables[key] = String(val);
}

When the JSON element contains nested objects or arrays - which is common in real API responses - the template variables for those keys silently become useless strings:

  • A nested object becomes [object Object].
  • A nested array becomes its comma-joined Array.prototype.toString() form, including nested-object members which themselves render as [object Object].
  • null becomes the literal string null.

The agent ends up with a garbled prompt and no warning that the data was lost.

Expected behavior

Either:

  1. Stringify nested values as JSON so the agent receives a faithful representation (for example JSON.stringify(val) for non-string/number/boolean values), or
  2. Throw a clear error when a referenced placeholder maps to a non-stringifiable value, so users know to pre-process their data or use path/idField to flatten.

Option 1 is the lower-friction change and matches what users intuitively expect when iterating over a JSON file: any field they reference in the template appears in the prompt.

Minimal reproduction

import { JsonPromptGenerator } from 'loop-the-loop/prompt-generators/json';
import { LoopState } from 'loop-the-loop/util/loop-state';

const generator = await JsonPromptGenerator.create({
  data: [
    {
      id: '1',
      reporter: { name: 'Alice', team: 'Web' },
      tags: ['bug', 'a11y'],
    },
  ],
  idField: 'id',
  promptTemplate: 'Reporter: {{reporter}}, tags: {{tags}}',
});

for await (const prompt of generator.generate(new LoopState('ignored.json'))) {
  console.log(prompt.prompt);
  // → "Reporter: [object Object], tags: bug,a11y"
}

Suggested fix

In buildVariables, replace String(val) with a helper that stringifies non-scalars as JSON, for example:

function stringifyField(val: unknown): string {
  if (val === null || val === undefined) {
    return '';
  }
  if (typeof val === 'string') {
    return val;
  }
  if (typeof val === 'number' || typeof val === 'boolean') {
    return String(val);
  }
  return JSON.stringify(val);
}

Add a test that exercises a nested object field through promptTemplate.

Dominant language
TypeScript
Stars
2
Forks
1
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from joewalker/loop-the-loop

All issues in joewalker/loop-the-loop

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.