vercel/ai · warning

Could not convert regex pattern at ${refs.currentPath.join('

Error message

Could not convert regex pattern at ${refs.currentPath.join('/')} to a flag-independent form! Falling back to the flag-ignorant source

What it means

When converting a zod string regex check into a JSON Schema pattern, the converter tries to strip flags (like 'i' or 'm') into an inline flag-independent form. If the rewritten regex is not valid, it warns and falls back to the raw source, meaning flags are silently dropped in the JSON Schema pattern.

Source

Thrown at packages/provider-utils/src/to-json-schema/zod3-to-json-schema/parsers/string.ts:417

    if (flags.s && source[i] === '.') {
      pattern += inCharGroup ? `${source[i]}\r\n` : `[${source[i]}\r\n]`;
      continue;
    }

    pattern += source[i];
    if (source[i] === '\\') {
      isEscaped = true;
    } else if (inCharGroup && source[i] === ']') {
      inCharGroup = false;
    } else if (!inCharGroup && source[i] === '[') {
      inCharGroup = true;
    }
  }

  try {
    new RegExp(pattern);
  } catch {
    console.warn(
      `Could not convert regex pattern at ${refs.currentPath.join(
        '/',
      )} to a flag-independent form! Falling back to the flag-ignorant source`,
    );
    return regex.source;
  }

  return pattern;
}

View on GitHub (pinned to 69428b1f8b)

Solutions

  1. Rewrite the regex to be flag-independent (e.g. embed (?i) semantics manually, or use (?s)/(?m) style constructs supported by the target).
  2. Handle case-insensitivity outside the schema (e.g. normalize input with toLowerCase before validation) and use a flagless regex.
  3. Accept the fallback and apply the flag semantics at the application level after schema validation.
  4. Check the produced JSON Schema and manually patch the 'pattern' property with the correct flag-aware form.

Example fix

// before
z.string().regex(/^[a-z]+$/i)
// after
z.string().regex(/^[a-zA-Z]+$/) // flag-independent, converts cleanly
Defensive patterns

Strategy: validation

Validate before calling

// ensure regexes convert flag-independently before use
schema._def.checks?.forEach(c => {
  if (c.kind === 'regex' && (c.regex.flags)) console.warn('flagged regex in schema', c.regex);
});

Prevention

When it happens

Trigger: zodToJsonSchema conversion of a schema containing z.string().regex(...) where the regex uses flags that cannot be represented flag-independently, and the synthesized pattern fails `new RegExp(pattern)` validation.

Common situations: Case-insensitive or multiline regex checks in zod schemas being converted for generateObject/tool schemas; complex regexes with character classes or quantifiers that break the flag-inlining rewrite.

Related errors


AI-assisted analysis of vercel/ai@69428b1f8b (2026-08-30). Data as JSON: /api/errors/1cee2e0884dbd85f. Report an issue: GitHub.