fix(schema): remove redundant code field from JSON output in favor of multi-system codes (icd10cm, icdo, cpt)
This commit is contained in:
@@ -370,7 +370,6 @@ For every matched condition, the engine outputs deterministic structured JSON ad
|
|||||||
|
|
||||||
```json
|
```json
|
||||||
{
|
{
|
||||||
"code": "I50.21",
|
|
||||||
"description": "Acute systolic (congestive) heart failure",
|
"description": "Acute systolic (congestive) heart failure",
|
||||||
"role": "PRIMARY",
|
"role": "PRIMARY",
|
||||||
"acuity": "ACUTE",
|
"acuity": "ACUTE",
|
||||||
@@ -388,7 +387,6 @@ If an oncology morphology or procedure is matched, the corresponding `icdo` or `
|
|||||||
|
|
||||||
```json
|
```json
|
||||||
{
|
{
|
||||||
"code": "C50.911",
|
|
||||||
"description": "Malignant neoplasm of unspecified site of right female breast",
|
"description": "Malignant neoplasm of unspecified site of right female breast",
|
||||||
"role": "PRIMARY",
|
"role": "PRIMARY",
|
||||||
"acuity": "UNSPECIFIED",
|
"acuity": "UNSPECIFIED",
|
||||||
|
|||||||
@@ -795,7 +795,6 @@ def finalize_output_node(state: PipelineGraphState) -> dict[str, Any]:
|
|||||||
cpt_val = item.cpt if item.cpt else (item.code if is_cpt else None)
|
cpt_val = item.cpt if item.cpt else (item.code if is_cpt else None)
|
||||||
|
|
||||||
resp = CodedDiagnosisResponse(
|
resp = CodedDiagnosisResponse(
|
||||||
code=item.code,
|
|
||||||
description=item.description,
|
description=item.description,
|
||||||
role=item.role,
|
role=item.role,
|
||||||
acuity=Acuity.ACUTE
|
acuity=Acuity.ACUTE
|
||||||
|
|||||||
@@ -16,8 +16,7 @@ from medical_coding.schemas.validation import AbstentionRecord
|
|||||||
class CodedDiagnosisResponse(BaseModel):
|
class CodedDiagnosisResponse(BaseModel):
|
||||||
"""External deterministic representation of an evidence-backed ICD code decision."""
|
"""External deterministic representation of an evidence-backed ICD code decision."""
|
||||||
|
|
||||||
code: str = Field(description="Authoritative ICD-10-CM code verified against catalog.")
|
description: str = Field(description="Official clinical description matching the code.")
|
||||||
description: str = Field(description="Official ICD-10-CM description.")
|
|
||||||
role: DiagnosisRole = Field(description="PRIMARY or SECONDARY.")
|
role: DiagnosisRole = Field(description="PRIMARY or SECONDARY.")
|
||||||
acuity: Acuity = Field(description="Acuity status (e.g. ACUTE, CHRONIC).")
|
acuity: Acuity = Field(description="Acuity status (e.g. ACUTE, CHRONIC).")
|
||||||
certainty: Certainty = Field(description="Certainty status (e.g. CONFIRMED, SUSPECTED).")
|
certainty: Certainty = Field(description="Certainty status (e.g. CONFIRMED, SUSPECTED).")
|
||||||
@@ -46,6 +45,26 @@ class CodedDiagnosisResponse(BaseModel):
|
|||||||
description="Matched CPT procedural code if matched, None if not.",
|
description="Matched CPT procedural code if matched, None if not.",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@model_validator(mode="before")
|
||||||
|
@classmethod
|
||||||
|
def handle_legacy_code(cls, data: Any) -> Any:
|
||||||
|
"""Allow legacy 'code' kwarg to seamlessly map to appropriate system field without being redundant in output."""
|
||||||
|
if isinstance(data, dict):
|
||||||
|
code_val = data.pop("code", None)
|
||||||
|
if code_val and not data.get("icd10cm") and not data.get("icdo") and not data.get("cpt"):
|
||||||
|
if str(code_val).startswith("M") and len(str(code_val)) >= 5:
|
||||||
|
data["icdo"] = code_val
|
||||||
|
elif str(code_val).isdigit() and len(str(code_val)) in (4, 5):
|
||||||
|
data["cpt"] = code_val
|
||||||
|
else:
|
||||||
|
data["icd10cm"] = code_val
|
||||||
|
return data
|
||||||
|
|
||||||
|
@property
|
||||||
|
def code(self) -> str:
|
||||||
|
"""Dynamic code property resolving primary matched code for backward compatibility."""
|
||||||
|
return self.icd10cm or self.icdo or self.cpt or ""
|
||||||
|
|
||||||
|
|
||||||
class CodingResult(BaseModel):
|
class CodingResult(BaseModel):
|
||||||
"""Deterministic final output conforming to clinical coding architecture principles."""
|
"""Deterministic final output conforming to clinical coding architecture principles."""
|
||||||
|
|||||||
+2
-1
@@ -59,4 +59,5 @@ def test_code_pdf_success_endpoint(test_client: TestClient) -> None:
|
|||||||
assert "document_id" in data
|
assert "document_id" in data
|
||||||
assert data["status"] in ("SUCCESS", "PARTIAL_SUCCESS")
|
assert data["status"] in ("SUCCESS", "PARTIAL_SUCCESS")
|
||||||
assert data["primary_diagnosis"] is not None
|
assert data["primary_diagnosis"] is not None
|
||||||
assert data["primary_diagnosis"]["code"] == "I50.21"
|
assert data["primary_diagnosis"]["icd10cm"] == "I50.21"
|
||||||
|
assert "code" not in data["primary_diagnosis"]
|
||||||
|
|||||||
@@ -107,16 +107,16 @@ def test_json_output_format_with_multisystem_fields() -> None:
|
|||||||
)
|
)
|
||||||
|
|
||||||
dumped = diag.model_dump()
|
dumped = diag.model_dump()
|
||||||
assert dumped["code"] == "I50.21"
|
assert "code" not in dumped
|
||||||
assert dumped["icd10cm"] == "I50.21"
|
assert dumped["icd10cm"] == "I50.21"
|
||||||
assert dumped["icdo"] is None
|
assert dumped["icdo"] is None
|
||||||
assert dumped["cpt"] is None
|
assert dumped["cpt"] is None
|
||||||
assert dumped["is_terminal_billable"] is True
|
assert dumped["is_terminal_billable"] is True
|
||||||
assert dumped["confidence_score"] == 0.96
|
assert dumped["confidence_score"] == 0.96
|
||||||
|
assert diag.code == "I50.21"
|
||||||
|
|
||||||
# Test oncology scenario with morphology code
|
# Test oncology scenario with morphology code
|
||||||
diag_onc = CodedDiagnosisResponse(
|
diag_onc = CodedDiagnosisResponse(
|
||||||
code="C50.911",
|
|
||||||
description="Malignant neoplasm of unspecified site of right female breast",
|
description="Malignant neoplasm of unspecified site of right female breast",
|
||||||
role=DiagnosisRole.PRIMARY,
|
role=DiagnosisRole.PRIMARY,
|
||||||
acuity=Acuity.UNSPECIFIED,
|
acuity=Acuity.UNSPECIFIED,
|
||||||
@@ -130,7 +130,8 @@ def test_json_output_format_with_multisystem_fields() -> None:
|
|||||||
)
|
)
|
||||||
|
|
||||||
dumped_onc = diag_onc.model_dump()
|
dumped_onc = diag_onc.model_dump()
|
||||||
assert dumped_onc["code"] == "C50.911"
|
assert "code" not in dumped_onc
|
||||||
assert dumped_onc["icd10cm"] == "C50.911"
|
assert dumped_onc["icd10cm"] == "C50.911"
|
||||||
assert dumped_onc["icdo"] == "M8500.3"
|
assert dumped_onc["icdo"] == "M8500.3"
|
||||||
assert dumped_onc["cpt"] == "19120"
|
assert dumped_onc["cpt"] == "19120"
|
||||||
|
assert diag_onc.code == "C50.911"
|
||||||
|
|||||||
Reference in New Issue
Block a user