fix(api): strip NUL bytes from LLM output fields before API response#3221
Open
firecrawl-spring[bot] wants to merge 2 commits intomainfrom
Open
fix(api): strip NUL bytes from LLM output fields before API response#3221firecrawl-spring[bot] wants to merge 2 commits intomainfrom
firecrawl-spring[bot] wants to merge 2 commits intomainfrom
Conversation
LLM-generated output (summary, extract/json, answer) could contain NUL bytes (\u0000) when the model mishandles special characters like ² (U+00B2). These NUL bytes pass through to the API response unchanged, causing downstream failures for users storing results in PostgreSQL text columns (which reject NUL bytes). Add removeNulBytes() helper that recursively strips NUL bytes from strings, arrays, and objects, and apply it to all three LLM output assignment paths in llmExtract.ts. Co-Authored-By: micahstairs <micah@sideguide.dev>
Contributor
|
Found 6 test failures on Blacksmith runners: Failures
|
Replace manual recursion through arrays and objects with JSON.stringify's built-in replacer function, which handles all nesting automatically. This eliminates the separate array/object branches while maintaining identical behavior. Co-Authored-By: gaurav <gauravchadha1676@gmail.com>
Chadha93
approved these changes
Mar 25, 2026
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.
Summary
LLM-generated output fields (
summary,extract/json,answer) could contain NUL bytes (\u0000) when the model mishandles special characters like²(U+00B2). These NUL bytes pass through to the API response unchanged, causing downstream failures for users storing results in PostgreSQL text columns (which reject NUL bytes).Changes
removeNulBytes()helper inllmExtract.tsthat strips NUL bytes from strings, and usesJSON.stringifywith a replacer to handle nested arrays/objects without manual recursiondocument.extract/document.json(extract data)document.summary(summary generation)document.answer(query generation)This is consistent with existing sanitization patterns in the codebase:
sanitizeMetadataValue()inbuild-document.tsstrips control chars via.replace(/[\x00-\x1f\x7f]/g, '')sanitizeString()inlog_job.tsstrips NUL via.replace(/\u0000/g, '')Updates since last revision
removeNulBytes()implementation: replaced manual recursion through arrays and objects withJSON.stringify's built-in replacer function. This eliminates the separate array/object branches while maintaining identical behavior. The replacer operates on actual string values (not serialized form), so there are no edge cases with escaped characters.Test plan
removeNulBytes()logic with strings containing\u0000, nested objects, arrays, null/undefined, numbers, and literal\u0000text — all pass correctlyReview & Testing Checklist for Human
JSON.stringifyreplacer approach doesn't silently drop or alter non-string values in deeply nested extract output (e.g. numbers, booleans, nulls inside arrays of objects)²,³, or other superscript characters) to verify the fix end-to-endNotes
JSON.stringify/JSON.parseround-trip is safe for LLM extract output since it's already JSON-parsed data (no Dates, undefined values, or class instances to lose)modelChaininperformQueryis from prettier (pre-commit hook), not a functional changeRelated Pylon Ticket
https://app.usepylon.com/issues?issueNumber=25035
Link to Devin session: https://app.devin.ai/sessions/dac229a885894c6d8d87db047fe8d440
Requested by: @Chadha93