SubtitleEdit/subtitleedit · warning · Exception

Could not find matching AF_initDataCallback

Error message

Could not find matching AF_initDataCallback

What it means

Thrown by Core.GetAFData when the regex AF_initDataCallback({.*?}) finds no match whose value contains 'DetectedObject'. Lens embeds OCR results inside an AF_initDataCallback JS call tagged with DetectedObject; if none is present, either Lens found no text or the response structure changed. The matches are dumped to the console before throwing to aid debugging.

Source

Thrown at src/ui/Logic/Ocr/GoogleLens/Core.cs:324

            {
                if (DateTimeOffset.TryParse(subParts[1].Trim(), out var expires))
                {
                    cookie.Expires = expires.ToUnixTimeMilliseconds();
                }
            }
        }

        return cookie;
    }

    public static JObject? GetAFData(string text)
    {
        var matches = Regex.Matches(text, @"AF_initDataCallback\((\{.*?\})\)", RegexOptions.Singleline);
        var lensCallback = matches.FirstOrDefault(m => m.Value.Contains("DetectedObject"));
        if (lensCallback == null)
        {
            Console.WriteLine(matches);
            throw new Exception("Could not find matching AF_initDataCallback");
        }

        var json = lensCallback.Groups[1].Value;
        return JObject.Parse(json);
    }

    public static List<string> ParseResult(JObject afData, int[] imageDimensions)
    {
        var data = afData["data"];
        var fullTextPart = data?[3];
        var textSegments = new List<string>();
        var textRegions = new List<double[]>();

        try
        {
            // Method 1: get text segments and regions directly
            if (fullTextPart?[4]?[0]?[0] != null)
            {

View on GitHub (pinned to 17a9f07487)

Solutions

  1. Check the console output of matches to see what callbacks Lens did return.
  2. Treat 'no DetectedObject' as 'no text found' rather than a hard error for blank images.
  3. Update the regex / marker string if Google renamed DetectedObject.
  4. Verify the image actually contains text; retry with a higher-contrast crop.

Example fix

// before
var af = Core.GetAFData(text);

// after
var af = Core.GetAFData(text); // wrap:
try { var af = Core.GetAFData(text); }
catch (Exception ex) when (ex.Message.Contains("AF_initDataCallback")) { return new List<string>(); }
Defensive patterns

Strategy: try-catch

Type guard

static bool HasDetectedObjectCallback(string text) => Regex.IsMatch(text, @"AF_initDataCallback\(\{.*?DetectedObject.*?\}\)", RegexOptions.Singleline);

Try / catch

try { var af = Core.GetAFData(text); }
catch (Exception ex) when (ex.Message.Contains("AF_initDataCallback")) { return new List<string>(); // treat as no text found }

Prevention

When it happens

Trigger: A 200 Lens response with no detected objects (blank/no-text image), a response that is a captcha/interstitial, or a schema change where the DetectedObject marker was renamed. Also when the response is HTML error content.

Common situations: OCR'ing an image with no recognizable text; Google changed the callback key; the response is a consent/captcha page returned with status 200; regional response variants.

Related errors


AI-assisted analysis of SubtitleEdit/subtitleedit@17a9f07487 (2026-08-13). Data as JSON: /api/errors/f3dd3c5f35686c55. Report an issue: GitHub.