Add batch support to /anonymize and /deanonymize endpoints - #2233
Open
bijay-odyssey wants to merge 1 commit into
Open
Add batch support to /anonymize and /deanonymize endpoints#2233bijay-odyssey wants to merge 1 commit into
bijay-odyssey wants to merge 1 commit into
Conversation
presidio-analyzer's /analyze endpoint already accepts a list of texts and returns a matching list of results. presidio-anonymizer's /anonymize and /deanonymize never got the same treatment, even though the core package already ships BatchAnonymizerEngine and BatchDeanonymizeEngine unused by the HTTP layer. Extend both endpoints so 'text' can be a string (existing behavior, unchanged) or an array of strings. In array mode, 'analyzer_results' / 'anonymizer_results' must be an array of the same length, one list of results per text, and the response becomes a matching array of results. A length mismatch raises a 422 with a clear message instead of failing silently or misaligning results. Adds tests/test_app.py (no HTTP-level test file existed for this service before), covering single-text behavior is unchanged, batch anonymize/deanonymize including a full encrypt/decrypt round trip, missing-results defaulting, and length-mismatch validation. Updates docs/api-docs/api-docs.yml (request/response schemas + batch examples) to match how /analyze's existing batch support is already documented there. Closes data-privacy-stack#1045
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.
Closes #1045.
Problem
/analyzealready acceptstextas either a single string or an array of strings, returning a matching array of results when given a batch (viaBatchAnalyzerEngine)./anonymizeand/deanonymizenever got the same treatment — even though the core package already shipsBatchAnonymizerEngineandBatchDeanonymizeEngine, unused by the HTTP layer.Change
Both endpoints now accept
textas a string (unchanged, existing behavior) or an array of strings. In array mode:analyzer_results(for/anonymize) /anonymizer_results(for/deanonymize) must be an array of the same length astext, one list of per-text results.textand the results array raises a clear422instead of silently misaligning or dropping data.I went with looping directly over the engines in
app.py(rather than reusingBatchAnonymizerEngine.anonymize_list/BatchDeanonymizeEngine.deanonymize_list) because those methods return only the anonymized text string, droppingitems— which would make the batch response shape inconsistent with the single-text response. Looping directly preserves full response parity between single and batch modes.Testing
Added
presidio-anonymizer/tests/test_app.py(no HTTP-level test file existed for this service before) using Flask's test client, covering:analyzer_resultsdefaults correctlyFull existing suite: 282 passed, 0 regressions.
ruff checkclean.Docs
Updated
docs/api-docs/api-docs.yml(request/response schemas + a batch example) for both endpoints, mirroring exactly how/analyze's existing batch support is already documented there. Validated withopenapi-spec-validator.