Issue #3586535: AI Automators strip unresolved tokens from prompts
Description
When using AI Automators with token-based prompts and optional fields, unresolved tokens are left as raw text in the prompt sent to the LLM. If field_user_prompt is empty, the LLM receives, sample system prompt:
Write a summary. Extra instructions: [node:field_user_prompt:value]
The literal bracket syntax is sent to the provider instead of being stripped.
Why
Drupal's token->replace() leaves any token it cannot resolve in place. The clear option exists specifically to strip these, but it is not being used. Sending unresolved tokens to the LLM wastes input tokens, adds noise, and exposes internal Drupal field names to the provider as per the context of the issue.
How
Add ['clear' => TRUE] as the third argument as per recommendation by Marcus to token->replace() in AiPromptHelper::renderTokenPrompt()
Resolved tokens still substitute correctly. Unresolved tokens are stripped before the prompt reaches the provider.
Testing instructions
I've used the module ai_test as this is not required credits to test.
Use xdebug to check before and after the fix/changes:
Before:
$beforeFix = $this->token->replace($prompt, [
$this->getEntityTokenType($entity->getEntityTypeId()) => $entity,
'user' => $this->currentUser,
]);Result: Write a summary. Extra instructions: [node:field_optional_text_field:value]
After:
$afterFix = $this->token->replace($prompt, [
$this->getEntityTokenType($entity->getEntityTypeId()) => $entity,
'user' => $this->currentUser,
], ['clear' => TRUE]);Result: Write a summary. Extra instructions:
Run the existing unit test in Drupal core 11.4.4
Unit test results: Tests: 40, Assertions: 191, PHPUnit Deprecations: 7.
Kernel test results: Tests: 42, Assertions: 520, Deprecations: 23, PHPUnit Deprecations: 46.
Checklist
- I have linked the related issue in the MR title or description
- I have performed a self-review of my own code
- I have added or updated tests, or explained in the description why this change is not covered by tests
- I have updated documentation for any new or changed functionality
- I have written testing instructions and verified them locally
- I have noted any required post-merge steps (config imports, cache rebuilds, manual changes)
- This MR contains no breaking API or hook changes, or they are explicitly documented in the description
AI Compliance
Note
Check the one that best describes your usage, or leave all unchecked if AI was not significantly used.
- AI Assisted Code
Mainly written by a human; AI used for autocomplete or partial generation under full human supervision. - AI Generated Code
Mainly generated by AI, reviewed and approved by a human before this MR was created. - Vibe Coded
Generated by AI and only functionally reviewed before this MR was created.
Closes #3586535