Latest updates
This commit is contained in:
@@ -806,3 +806,94 @@ def normalize_to_json_list(val):
|
||||
result = json.dumps(expanded)
|
||||
return result
|
||||
return val
|
||||
|
||||
|
||||
def normalize_one_to_one_field_value(field_name: str, value) -> str | list:
|
||||
"""
|
||||
Normalize a 1:1 field value to ensure single-value fields are strings, not lists.
|
||||
|
||||
This function handles the common case where LLM returns a single-value field as
|
||||
a list with one element (e.g., ['The Value']). It extracts the string from such
|
||||
lists while preserving legitimate multi-value lists.
|
||||
|
||||
Args:
|
||||
field_name (str): Name of the field being normalized (for logging/debugging)
|
||||
value: The field value to normalize. Can be:
|
||||
- str: Returned as-is
|
||||
- list with 1 element: Extract the string
|
||||
- list with >1 elements: Returned unchanged (may be legitimate multi-value)
|
||||
- None/empty: Returned as-is
|
||||
|
||||
Returns:
|
||||
str | list: Normalized value:
|
||||
- If input is a list with exactly 1 element: returns that element as string
|
||||
- If input is already a string: returns as-is
|
||||
- If input is a list with >1 elements: returns list unchanged
|
||||
- If input is None or empty: returns as-is
|
||||
|
||||
Examples:
|
||||
>>> normalize_one_to_one_field_value("CONTRACT_TITLE", ["Provider Agreement"])
|
||||
'Provider Agreement'
|
||||
>>> normalize_one_to_one_field_value("PAYER_NAME", "Aetna")
|
||||
'Aetna'
|
||||
>>> normalize_one_to_one_field_value("PROVIDER_NAME", ["Provider A", "Provider B"])
|
||||
['Provider A', 'Provider B'] # Multi-value preserved
|
||||
>>> normalize_one_to_one_field_value("LOB", ["Medicare", "Medicaid"])
|
||||
['Medicare', 'Medicaid'] # Multi-value preserved
|
||||
"""
|
||||
# Handle None and empty values
|
||||
if value is None or (isinstance(value, str) and is_empty(value)):
|
||||
return value
|
||||
|
||||
# If it's already a string, return as-is
|
||||
if isinstance(value, str):
|
||||
return value
|
||||
|
||||
# If it's a list
|
||||
if isinstance(value, list):
|
||||
# List with exactly 1 element: extract the string
|
||||
if len(value) == 1:
|
||||
return str(value[0]) if value[0] is not None else "N/A"
|
||||
# List with >1 elements: preserve (may be legitimate multi-value)
|
||||
# This handles cases like PROVIDER_NAME with multiple providers,
|
||||
# or dynamic fields (LOB, PROGRAM, PRODUCT, NETWORK) when passed to 1:1
|
||||
elif len(value) > 1:
|
||||
return value
|
||||
# Empty list: return "N/A"
|
||||
else:
|
||||
return "N/A"
|
||||
|
||||
# For any other type, convert to string
|
||||
return str(value) if value is not None else "N/A"
|
||||
|
||||
|
||||
def normalize_one_to_one_answers_dict(answers_dict: dict[str, str | list]) -> dict[str, str | list]:
|
||||
"""
|
||||
Normalize all values in a 1:1 answers dictionary.
|
||||
|
||||
Applies normalization to all fields in the dictionary, converting single-element
|
||||
lists to strings while preserving legitimate multi-value lists.
|
||||
|
||||
Args:
|
||||
answers_dict (dict): Dictionary of field names to values (may be strings or lists)
|
||||
|
||||
Returns:
|
||||
dict: Dictionary with normalized values (single-element lists converted to strings)
|
||||
|
||||
Example:
|
||||
>>> answers = {
|
||||
... "CONTRACT_TITLE": ["Provider Agreement"],
|
||||
... "PAYER_NAME": "Aetna",
|
||||
... "PROVIDER_NAME": ["Provider A", "Provider B"]
|
||||
... }
|
||||
>>> normalize_one_to_one_answers_dict(answers)
|
||||
{
|
||||
"CONTRACT_TITLE": "Provider Agreement",
|
||||
"PAYER_NAME": "Aetna",
|
||||
"PROVIDER_NAME": ["Provider A", "Provider B"]
|
||||
}
|
||||
"""
|
||||
normalized = {}
|
||||
for field_name, value in answers_dict.items():
|
||||
normalized[field_name] = normalize_one_to_one_field_value(field_name, value)
|
||||
return normalized
|
||||
|
||||
Reference in New Issue
Block a user