[DOCS] Add programmatic solutions for "Trailing Whitespace" error in Assistant prefilling
Documentation Type
Unclear/confusing documentation
Documentation Location
URL: https://platform.claude.com/docs/en/build-with-claude/prompt-engineering/prefill-claudes-response
Section/Topic
Section: "How to prefill Claude's response"
Current Documentation
"The prefill content cannot end with trailing whitespace. A prefill like 'As an AI assistant, I ' (with a space at the end) will result in an error."
What's Wrong or Missing?
The documentation correctly identifies a common source of 400 Bad Request errors, but it leaves the burden of sanitization entirely on the developer without providing the standard code patterns to avoid it.
When developers programmatically build prefill strings—especially when using multi-line strings, template literals, or concatenating JSON braces—it is extremely easy to accidentally include a trailing space or newline. Providing a warning without showing the idiomatic SDK "fix" (like .rstrip() or .trimEnd()) leads to a higher friction integration experience.
Suggested Improvement
Add a "Pro-Tip" or a code-snippet update that demonstrates how to sanitize strings before passing them to the Messages API.
Suggested Text Addition:
Tip: Sanitize your prefill strings If you are generating your prefill content dynamically, use your language's string trimming methods to ensure you don't accidentally send trailing whitespace and trigger a validation error. Python: ``python prefill = "Here is the JSON: { ".rstrip()`**TypeScript:**`typescript const prefill = "Here is the JSON: { ".trimEnd()``
Impact
High - Prevents users from using a feature
Additional Context
- Related documentation: Messages API Reference
- Context: In many templating systems (like Jinja or f-strings), trailing newlines are a default behavior. Without a clear "best practice" snippet in the docs, developers often have to debug 400 errors via trial and error. Adding this to the SDK examples would significantly improve the developer experience for one of Claude's most powerful features (prefilling).
This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗