The personal finance app for everyone (by everyone)
Ruby
fix(anthropic): stringify symbol keys in PDF extracted_data (#4875)
Saim Shafiqued740a730cf25
diff --git a/app/models/provider/anthropic/pdf_processor.rb b/app/models/provider/anthropic/pdf_processor.rbindex f8e31b1477..6972b5381e 100644--- a/app/models/provider/anthropic/pdf_processor.rb+++ b/app/models/provider/anthropic/pdf_processor.rb@@ -156,10 +156,15 @@ class Provider::Anthropic::PdfProcessorenddef build_result(parsed)+ # The Anthropic SDK hands tool input back with symbol keys, while the+ # OpenAI path JSON-parses to string keys. Stringify here so every+ # reader of the result can dig with strings; without this the AI+ # health probe reads nil for institution_name and always fails.+ extracted_data = parsed["extracted_data"] || parsed[:extracted_data]PdfProcessingResult.new(summary: parsed["summary"] || parsed[:summary],document_type: normalize_document_type(parsed["document_type"] || parsed[:document_type]),- extracted_data: parsed["extracted_data"] || parsed[:extracted_data] || {}+ extracted_data: extracted_data.is_a?(Hash) ? extracted_data.deep_stringify_keys : {})enddiff --git a/test/models/ai_health/probe_test.rb b/test/models/ai_health/probe_test.rbindex c7e0f239b8..364431d524 100644--- a/test/models/ai_health/probe_test.rb+++ b/test/models/ai_health/probe_test.rb@@ -240,13 +240,15 @@ class AiHealth::ProbeTest < ActiveSupport::TestCaseendtest "PDF vision probe sends the synthetic PDF as an Anthropic document block" do+ # Symbol keys, as the Anthropic SDK returns tool input (the JSON.parse+ # branch is skipped); string keys here would not reproduce the probe.tool_use = Struct.new(:type, :input).new(:tool_use,{- "document_type" => "bank_statement",- "summary" => "A synthetic bank statement containing no customer data.",- "extracted_data" => {- "institution_name" => AiHealth::Probe::PDF_TEST_INSTITUTION+ document_type: :bank_statement,+ summary: "A synthetic bank statement containing no customer data.",+ extracted_data: {+ institution_name: AiHealth::Probe::PDF_TEST_INSTITUTION}})diff --git a/test/models/provider/anthropic/pdf_processor_test.rb b/test/models/provider/anthropic/pdf_processor_test.rbindex 5ac664eacd..6bc01fbb52 100644--- a/test/models/provider/anthropic/pdf_processor_test.rb+++ b/test/models/provider/anthropic/pdf_processor_test.rb@@ -68,6 +68,39 @@ class Provider::Anthropic::PdfProcessorTest < ActiveSupport::TestCaseassert_equal 42, result.extracted_data["transaction_count"]end+ test "stringifies symbol keys in extracted_data" do+ # The Anthropic SDK returns tool input with symbol keys, so the JSON.parse+ # branch is skipped; without stringifying, every string-keyed reader of+ # the result (e.g. the AI health probe) sees nil.+ fake_response = build_response(content: [+ tool_use_block(+ id: "toolu_1",+ name: "report_document_analysis",+ input: {+ document_type: :bank_statement,+ summary: "Bank of Example, Mar 2026 statement.",+ extracted_data: {+ institution_name: "Bank of Example",+ transaction_count: 42,+ account_holder: "Account Holder"+ }+ }+ )+ ])+ client = stub_client(fake_response)++ result = Provider::Anthropic::PdfProcessor.new(+ client,+ model: "claude-sonnet-4-6",+ pdf_content: @pdf_content+ ).process++ assert_equal "bank_statement", result.document_type+ assert_equal "Bank of Example", result.extracted_data["institution_name"]+ assert_equal 42, result.extracted_data["transaction_count"]+ assert_equal "Account Holder", result.extracted_data["account_holder"]+ end+test "normalizes unknown document_type to other" dofake_response = build_response(content: [tool_use_block(