{"record":{"id":"df143bb015f4b945","repo":"alibaba/spring-ai-alibaba","slug":"detected-d-instance-s-of-s-in-text-content","errorCode":null,"errorMessage":"Detected %d instance(s) of %s in text content","messagePattern":"Detected (.+?) instance\\(s\\) of (.+?) in text content","errorType":"exception","errorClass":"PIIDetectionException","httpStatus":null,"severity":"error","filePath":"spring-ai-alibaba-agent-framework/src/main/java/com/alibaba/cloud/ai/graph/agent/hook/pii/PIIDetectionHook.java","lineNumber":133,"sourceCode":"\t\t\treturn new AgentCommand(previousMessages);\n\t\t}\n\n\t\tString content = aiMessage.getText();\n\n\t\tif (content == null || content.isEmpty()) {\n\t\t\treturn new AgentCommand(previousMessages);\n\t\t}\n\n\t\t// Detect PII\n\t\tProcessResult result = processText(content);\n\n\t\tif (!result.hasMatches) {\n\t\t\treturn new AgentCommand(previousMessages);\n\t\t}\n\n\t\t// Apply strategy\n\t\tif (result.hasMatches && strategy == RedactionStrategy.BLOCK) {\n\t\t\tthrow new PIIDetectionException(piiType.name(), result.matches);\n\t\t}\n\n\t\tif (result.redactedText.equals(content)) {\n\t\t\treturn new AgentCommand(previousMessages);\n\t\t}\n\n\t\t// Create updated message\n\t\tAssistantMessage updatedMessage = AssistantMessage.builder()\n\t\t\t.content(result.redactedText)\n\t\t\t.properties(aiMessage.getMetadata())\n\t\t\t.toolCalls(aiMessage.getToolCalls())\n\t\t\t.media(aiMessage.getMedia())\n\t\t\t.build();\n\n\t\tList<Message> updatedMessages = new ArrayList<>(previousMessages);\n\t\tupdatedMessages.set(lastIndex, updatedMessage);\n\n\t\treturn new AgentCommand(updatedMessages, UpdatePolicy.REPLACE);","sourceCodeStart":115,"sourceCodeEnd":151,"githubUrl":"https://github.com/alibaba/spring-ai-alibaba/blob/f82da0b50f35744c13968191be2b1cd2452ef550/spring-ai-alibaba-agent-framework/src/main/java/com/alibaba/cloud/ai/graph/agent/hook/pii/PIIDetectionHook.java#L115-L151","documentation":"PIIDetectionHook.afterModel() inspects model output text for the configured PII type. When matches are found and RedactionStrategy is BLOCK, it throws PIIDetectionException (\"Detected N instance(s) of TYPE in text content\") to stop the PII from propagating; the exception carries the PII type and match details.","triggerScenarios":"Configuring PIIDetectionHook with strategy RedactionStrategy.BLOCK and the model's afterModel output containing text matching the PII detector (e.g. email addresses, phone numbers, IPs).","commonSituations":"LLM echoing back user-supplied PII in its answer; summarization or RAG answers embedding contact details from documents; strict compliance pipelines that intentionally block rather than redact.","solutions":["Catch PIIDetectionException and handle per compliance policy (log, notify, retry with sanitization instructions).","Switch strategy to RedactionStrategy.REDACT so matched PII is masked instead of blocking the response.","Instruct/tune the model not to emit PII, or post-process prompts to remove PII from inputs that get echoed back.","If matches are false positives, adjust the detector or PII type configuration."],"exampleFix":"// before\nPIIDetectionHook hook = PIIDetectionHook.builder().piiType(PIIType.EMAIL).strategy(RedactionStrategy.BLOCK).build();\n// after\nPIIDetectionHook hook = PIIDetectionHook.builder().piiType(PIIType.EMAIL).strategy(RedactionStrategy.REDACT).build();","handlingStrategy":"try-catch","validationCode":"// pre-screen model output if you have access to it\n// (with BLOCK strategy the hook itself is the gate)\nboolean containsPii = PIIDetectors.emailDetector().detect(outputText).size() > 0;","typeGuard":null,"tryCatchPattern":"try {\n    result = agent.invoke(inputs);\n} catch (PIIDetectionException e) {\n    log.warn(\"PII blocked: type={}, matches={}\", e.getPiiType(), e.getMatches());\n    // retry with REDACT strategy or sanitization prompt\n}","preventionTips":["Prefer REDACT strategy unless blocking is a hard compliance requirement","Sanitize sources (docs, tool outputs) before they reach the model","Log PIIDetectionException details (without the PII) for auditing","Tune detectors to reduce false-positive blocks"],"tags":["java","pii","compliance","redaction","model-output"],"backgroundTag":"schema-validation-failed","analyzedSha":"f82da0b50f35744c13968191be2b1cd2452ef550","analyzedAt":"2026-09-09T15:32:42.421Z","contentChangedAt":"2026-09-09T15:32:42.421Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}