fix: escape double quotes and backslashes in AI directive string arguments#725
Open
Zelys-DFKH wants to merge 2 commits into
Open
Conversation
…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
🦋 Changeset detectedLatest commit: c0f9d53 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
soberm
reviewed
May 12, 2026
| } from './ConversationType'; | ||
| import type { InferenceConfiguration } from './ModelType'; | ||
|
|
||
| const escapeGraphQLString = (str: string): string => |
Contributor
There was a problem hiding this comment.
Should this follow the same approach?
amplify-data/packages/data-schema/src/SchemaProcessor.ts
Lines 581 to 583 in 2d75518
Author
There was a problem hiding this comment.
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.
…rsationSchemaProcessor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Resolves aws-amplify/amplify-backend#2995
Problem
@conversationand@generationdirectives embed user-supplied strings directly into GraphQL SDL without escaping special characters. AnysystemPromptor tooldescriptioncontaining 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.tsgets a privateescapeGraphQLStringhelper 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 bothsystemPromptand tooldescription.SchemaProcessor.tshad the identical gap in the@generationdirective. That file already had anescapeGraphQlStringhelper (wrappingJSON.stringify) used for@validateand@sqlarguments, so@generationnow goes through the same function.Corresponding docs PR, if applicable: N/A
Validation
Added
packages/data-schema/__tests__/ai/ConversationSchemaProcessor.test.tswith 5 tests:@generationsystemPrompt with embedded double quotes@conversationsystemPrompt with embedded double quotes@conversationtool description with embedded double quotes\ncorrectlyAll 556 existing unit tests pass with 270 snapshots unchanged.
Checklist