Skip to content

docs: add TransformersPromptInjectionRouter page - #14050

Open
davidsbatista wants to merge 5 commits into
mainfrom
docs/transformers-prompt-injection-router
Open

davidsbatista wants to merge 5 commits into
mainfrom
docs/transformers-prompt-injection-router

Conversation

@davidsbatista

Copy link
Copy Markdown
Contributor

Related Issues

Proposed Changes:

Adds a docs page for TransformersPromptInjectionRouter, the new router in the transformers-haystack integration that detects prompt injections in chat messages and routes them to a benign or an injection output.

  • New page pipeline-components/routers/transformerspromptinjectionrouter.mdx, following transformerstextrouter.mdx
  • Added to the sidebar and the routers overview table
  • Added to the Guardrails page, with a short "Prompt injection" section

How did you test it?

Ran the "On its own" example against the default model to check the shown output.

Notes for the reviewer

  • Merge only after a transformers-haystack release that includes the component, otherwise the API reference link doesn't resolve.
  • hep_available is false since the component isn't on the platform yet.

@vercel

vercel Bot commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
haystack-docs Ready Ready Preview Dec 4, 2026 11:43am UTC

Request Review

@@ -0,0 +1,136 @@
---
title: "TransformersPromptInjectionRouter"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could we also add this page, the routers table row, and the Guardrails section to versioned_docs/version-3.5/, plus the entry in versioned_sidebars/version-3.5-sidebars.json? Otherwise it won't show up in the 3.5 docs. See #14007 for the same pattern.


By default, the component raises an exception if classification fails. Set `on_error="injection"` to route the messages to `injection` instead, so that a failure never lets unchecked messages through. The error is added to the `prompt_injection_error` field in the `meta` of the checked messages.

Authentication with a Hugging Face API token is only required to access private or gated models. You can pass the token at initialization with `token`, or set the `HF_API_TOKEN` or `HF_TOKEN` environment variable.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Lets move the token paragraph out of "Handling errors", maybe into its own short "Authentication" section or just before the API reference line. It isn't related to error handling.

@davidsbatista
davidsbatista marked this pull request as ready for review December 3, 2026 13:20
@davidsbatista
davidsbatista requested a review from a team as a code owner December 3, 2026 13:20
@davidsbatista
davidsbatista requested review from julian-risch and removed request for a team December 3, 2026 13:20

### Prompt injection

A prompt injection is text that tries to override the model's instructions, such as "Ignore all previous instructions". Put a [`TransformersPromptInjectionRouter`](../pipeline-components/routers/transformerspromptinjectionrouter.mdx) in front of a chat generator or an Agent. It checks the messages with a Hugging Face model trained to detect prompt injections, sends benign messages on, and routes injections to a separate output. To catch indirect injections, it can also check tool results, such as fetched web pages.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Placed in front of an Agent, I believe the router only sees the messages passed into the Agent, so tool results the Agent fetches during its run never reach it. Could we say where the router needs to sit to check tool results, or drop the indirect-injection sentence here? WDYT? (Same in version-3.5.)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I've dropped the last sentence:

"To catch indirect injections, it can also check tool results, such as fetched web pages."


### Checking tool results

Tool results, such as fetched web pages or search results, can contain indirect prompt injections. To check them, include `ChatRole.TOOL` in `roles`. With the default `last_message_only=True`, the component checks the last tool result and all the tool results right before it, so every result of parallel tool calls is checked.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could we add a sentence on where the tool messages come from in this setup? In front of an Agent the router only gets the incoming history, not the tool results produced during the Agent's run. (Same in version-3.5.)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I've added this sentence:

"The component only checks the messages it receives, so this works when the messages you pass in already contain tool results, such as the chat history from earlier turns. It doesn't see the tool results an Agent fetches during its run."


pipe = Pipeline()
pipe.add_component("injection_router", TransformersPromptInjectionRouter())
pipe.add_component("llm", OpenAIChatGenerator())

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Lets use OpenAIResponsesChatGenerator here like the Guardrails page examples, including the import on line 109 and the note on line 134. (Same in version-3.5.)


Authentication with a Hugging Face API token is only required to access private or gated models. You can pass the token at initialization with `token`, or set the `HF_API_TOKEN` or `HF_TOKEN` environment variable.

To see the full list of parameters, check out our [API reference](/reference/integrations-transformers#transformerspromptinjectionrouter).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nit: the API reference line now sits under "Authentication". Lets move it to the end of the Overview, before "Checking tool results".

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

thanks! It's under overview now

This branch was successfully deployed

1 active deployment
Preview — 563995e1 Deployed Dec 4, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants