Skip to content

Conversation

@cpinn
Copy link
Contributor

@cpinn cpinn commented Jan 8, 2026

Users would like to be able to reference an array when handling images.

This Pr does the following

  1. users who include variables that are arrays of images or files those will now attempt to be auto expanded
  2. nunjucks needs to match the custom behavior that was built for mustache to handle some parsing quirks. Namely html escaping needs to be turned off in the environment and objects should be forced to print their string information.

@cpinn cpinn force-pushed the caitlin/modify-nunjucks-parsing-to-match-custom-mustache-behavior branch from 97a2bde to 74e0bc1 Compare January 8, 2026 22:37
@cpinn cpinn closed this Jan 8, 2026
@cpinn cpinn reopened this Jan 8, 2026
throwOnUndefined,
});

// Intercept renderString to wrap objects with custom toString
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nunjucks doesn't really support the type of custom escaping we were doing for mustache. This implements some of the custom parsing we were doing just to make sure that [Object object] doesn't get printed out during the toString method. Kind of hacky but it works to match the behavior.

@cpinn cpinn force-pushed the caitlin/modify-nunjucks-parsing-to-match-custom-mustache-behavior branch from 77b2ee2 to d6a79e6 Compare January 8, 2026 23:49
@cpinn cpinn requested a review from ibolmo January 12, 2026 22:24
return new nunjucks.Environment(null, {
autoescape: true,
const env = new nunjucks.Environment(null, {
autoescape: false,
Copy link
Collaborator

@ibolmo ibolmo Jan 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'd double check our docs to make sure we call out that we don't escape by default in both template renderers. perhaps how to escape, for those that want to

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't call this out at all, never did which is why I was quite surprised by it.

expect(result).toBe(
'User: {"name":"Alice","age":30}, Items: ["a","b","c"]',
);
expect(result).not.toContain("[object Object]");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'd encourage a literal comparison, if possible. the not.toContain could be misleading since any value is allowed except "[object ...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The actual comparison is just in the line above

expect(result).toBe(
'User: {"name":"Alice","age":30}, Items: ["a","b","c"]',
);

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oohh woops. 👍🏻

{ templateFormat: "nunjucks" },
);
// Arrays output as comma-separated (Nunjucks default behavior)
expect(resultWithAccess).toBe("User: Bob, Age: 25, Items: 1,2,3");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i expected the array to be ["1", "2", "3"] 🤔 (json stringify)

Copy link
Contributor Author

@cpinn cpinn Jan 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't decided if I want to overwrite this or not because this is just the behavior of nunjucks.

if you have an array it doesn't get output as [object object] it just gets outputs the array as such, this is regular rendering. Its a different behavior than mustache above which would print out the array.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like mustache does do as I'd expected. 🤔

escape,
{ templateFormat: "nunjucks" },
);
expect(result).toBe('Data: {"nested":{"value":123,"items":["x","y"]}}');
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think what will be interesting is that nunjucks renders arrays by default as x,y but if a nested object is printed and that is determined to be an object it will use the custom parser that will json stringify the entire object.

@cpinn cpinn requested a review from ibolmo January 14, 2026 00:23
@cpinn cpinn changed the title modify nunjucks parsing to stop parsing to string as [Object object] Handle arrays of images when building prompts Jan 14, 2026
@cpinn cpinn force-pushed the caitlin/modify-nunjucks-parsing-to-match-custom-mustache-behavior branch from 22803a9 to 5df5f53 Compare January 14, 2026 05:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants