Skip to content

fix: escape double quotes and backslashes in AI directive string arguments#725

Open
Zelys-DFKH wants to merge 2 commits into
aws-amplify:mainfrom
Zelys-DFKH:fix/escape-double-quotes-in-conversation-schema
Open

fix: escape double quotes and backslashes in AI directive string arguments#725
Zelys-DFKH wants to merge 2 commits into
aws-amplify:mainfrom
Zelys-DFKH:fix/escape-double-quotes-in-conversation-schema

Conversation

@Zelys-DFKH
Copy link
Copy Markdown

Resolves aws-amplify/amplify-backend#2995

Problem

@conversation and @generation directives embed user-supplied strings directly into GraphQL SDL without escaping special characters. Any systemPrompt or tool description containing a double quote produces invalid SDL, breaking schema compilation.

Issue number, if available: aws-amplify/amplify-backend#2995

Changes

Both processors had the same root cause: string interpolation into directive arguments with no character escaping.

ConversationSchemaProcessor.ts gets a private escapeGraphQLString helper that escapes backslashes first, then double quotes, then newlines. Order matters: escaping backslashes first prevents " from being double-escaped to \\" instead of \". The helper is applied to both systemPrompt and tool description.

SchemaProcessor.ts had the identical gap in the @generation directive. That file already had an escapeGraphQlString helper (wrapping JSON.stringify) used for @validate and @sql arguments, so @generation now goes through the same function.

Corresponding docs PR, if applicable: N/A

Validation

Added packages/data-schema/__tests__/ai/ConversationSchemaProcessor.test.ts with 5 tests:

  • @generation systemPrompt with embedded double quotes
  • @conversation systemPrompt with embedded double quotes
  • @conversation tool description with embedded double quotes
  • Backslash escaping in systemPrompt
  • Regression guard: multiline systemPrompt continues to serialize \n correctly

All 556 existing unit tests pass with 270 snapshots unchanged.

Checklist

  • If this PR includes a functional change to the runtime or type-level behavior of the code, I have added or updated automated test coverage for this change.
  • If this PR requires a docs update, I have linked to that docs PR above.

…ments

`@conversation` and `@generation` directives interpolate user-supplied
strings directly into GraphQL SDL without escaping special characters.
Any systemPrompt or tool description containing a double quote or
backslash produces invalid SDL, breaking schema compilation.

- ConversationSchemaProcessor: add escapeGraphQLString helper (backslash
  first, then double quote, then newline) applied to systemPrompt and
  tool description
- SchemaProcessor: route @generation systemPrompt through the existing
  escapeGraphQlString (JSON.stringify) helper already used for @Validate
  and @SQL

Fixes aws-amplify/amplify-backend#2995
@Zelys-DFKH Zelys-DFKH requested a review from a team as a code owner May 11, 2026 22:44
@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented May 11, 2026

🦋 Changeset detected

Latest commit: c0f9d53

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@aws-amplify/data-schema Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

} from './ConversationType';
import type { InferenceConfiguration } from './ModelType';

const escapeGraphQLString = (str: string): string =>
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.

Should this follow the same approach?

function escapeGraphQlString(str: string) {
return JSON.stringify(str);
}

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Good catch. The @generation path already uses JSON.stringify and I should have been consistent here too. I'll swap the manual regex for JSON.stringify(str).slice(1, -1) to use the same core while keeping the call-site quoting pattern. Pushing the fix now.

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.

AWS Amplify AI schema compilation fails when double quotes (") are embedded within systemPrompt or description string fields.

2 participants