Skip to content

Redact secrets from logged completions - #8585

Open
sergiopaniego wants to merge 5 commits into
mainfrom
redact-logged-completions
Open

sergiopaniego wants to merge 5 commits into
mainfrom
redact-logged-completions

Conversation

@sergiopaniego

@sergiopaniego sergiopaniego commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

What does this PR do?

Trainers that log completions now replace credentials with <REDACTED> before the completions reach any log.

With tools and environments, a completion carries what the tool or environment returned, so a model that prints its environment puts the trainer's credentials (for example HF_TOKEN) in the logged completions. From there they reach the console, Trackio and W&B tables, the parquet files in output_dir, and the Hub dataset set with log_completions_hub_repo.

redact_secrets (in trl/trainer/utils.py) replaces:

  • the values of environment variables named like credentials (HF_TOKEN, *_API_KEY, *_SECRET*, *_PASSWORD), when they are at least 8 characters long
  • strings shaped like common tokens: Hugging Face, OpenAI style sk-, GitHub, AWS access key IDs and PEM private keys

It walks strings nested in lists and dicts, so conversational prompts and completions, tool messages and extra columns are covered. Images and numbers are left as they are.

It runs where each trainer collects the texts it logs, so every destination gets the redacted text:

Trainer Where
GRPOTrainer, RLOOTrainer prompts, completions and log_extra columns
DistillationTrainer, GOLDTrainer, IWOPDTrainer prompts and completions
AsyncGRPOTrainer, async distillation rollout worker prompts and completions passed to print_prompt_completions_sample

It's always on, with no option to turn it off. The cost is a string replace per secret on the logged texts, once per logging step.

The GRPO docs get a warning in the Agent Training section: don't give tools and environments credentials they don't need, and the redaction is a safety net, not a guarantee (a secret split or encoded in the output is not caught).

The approach follows agent harnesses that scrub their trajectories: Harbor and OpenHands replace the values of credential env vars, and Trackio's trace scrubber and LangSmith's secret anonymizer match token formats. The downstream sinks don't protect against this: W&B and Trackio tables store the text as is, and the Hub's secret scanning only sends an email and doesn't cover LFS files such as parquet.

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you read the contributor guideline, Pull Request section?
  • Was this discussed/approved via a GitHub issue? Please add a link to it if that's the case.
  • Did you make sure to update the documentation with your changes?
  • Did you write any new necessary tests?

Tests:

  • TestRedactSecrets in tests/test_utils.py: credential env values, token formats, a non-credential variable with a similar name (TOKENIZERS_PARALLELISM) left alone, and non-string values.
  • test_train_with_tools_log_completions_redacts_secrets in tests/test_grpo_trainer.py: a tool returns an API key from the environment during GRPO training, and the completions parquet holds <REDACTED> instead. It fails without the change.
  • End to end on a Linux GPU (HF Jobs, no token in the Job, fake secrets only): GRPO with Qwen3-0.6B calling a tool that reads an API key from the environment, RLOO and Distillation, with prompts holding a token shaped string, logging to the console, Trackio, W&B (offline) and the completion files. On main the fake secrets show up 111 times across those outputs, with this PR 0 times.

AI writing disclosure

  • No AI usage: the PR was written entirely by a human.
  • AI-assisted: some parts were suggested or improved by AI, but the PR was written and reviewed by a human.
  • AI-generated: the PR was mostly or fully generated by an AI tool.

Who can review?

Anyone in the community is free to review the PR once the tests have passed.


Note

Low Risk
Changes only affect logged text before export, not training or reward computation; low risk of functional regression aside from altered completion log content.

Overview
Adds automatic secret scrubbing for all completion logging paths so credentials never reach parquet files, W&B, Trackio, or the console unchanged.

A new redact_secrets helper in trl/trainer/utils.py walks nested strings, lists, and dicts and replaces credential-like environment variable values (names matching *_TOKEN, *_API_KEY, etc., when values are ≥8 chars) and common token shapes (HF, OpenAI sk-, GitHub, AWS, GCP, Slack, PEM private keys) with <REDACTED>. It is wired in at log-collection time for GRPO/RLOO (prompts, completions, and log_extra columns), Distillation, GOLD, IWOPD, AsyncGRPO, and the async distillation rollout worker—always on when logging, with no toggle.

GRPO agent-training docs now warn that tools inherit the trainer env and redaction is a safety net, not a full guarantee. Unit tests cover redact_secrets behavior and an end-to-end GRPO run where a tool reads an API key from the environment and logged completions show <REDACTED>.

Reviewed by Cursor Bugbot for commit 4e6a90f. Bugbot is set up for automated code reviews on this repo. Configure here.

@bot-ci-comment

Copy link
Copy Markdown

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

Pages changed in this PR:

This branch has not been deployed

No deployments
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.

1 participant