Skip to content

Commit 487020b

Browse files
authored
Fixing column mapping validation regex (Azure#39429)
1 parent 3a8b0c5 commit 487020b

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

sdk/evaluation/azure-ai-evaluation/azure/ai/evaluation/_evaluate/_evaluate.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ def _process_column_mappings(
525525

526526
processed_config: Dict[str, Dict[str, str]] = {}
527527

528-
unexpected_references = re.compile(r"\${(?!target\.|data\.).+?}")
528+
expected_references = re.compile(r"^\$\{(target|data)\.[a-zA-Z_]+\}$")
529529

530530
if column_mapping:
531531
for evaluator, mapping_config in column_mapping.items():
@@ -534,7 +534,7 @@ def _process_column_mappings(
534534

535535
for map_to_key, map_value in mapping_config.items():
536536
# Check if there's any unexpected reference other than ${target.} or ${data.}
537-
if unexpected_references.search(map_value):
537+
if not expected_references.search(map_value):
538538
msg = "Unexpected references detected in 'column_mapping'. Ensure only ${target.} and ${data.} are used."
539539
raise EvaluationException(
540540
message=msg,

sdk/evaluation/azure-ai-evaluation/tests/unittests/test_evaluate.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -383,18 +383,31 @@ def test_apply_column_mapping_target(self, json_data, inputs_mapping, response):
383383
assert "else2" in new_data_df.columns
384384
assert new_data_df["else2"][0] == "Another column 1"
385385

386-
def test_evaluate_invalid_evaluator_config(self, mock_model_config, evaluate_test_data_jsonl_file):
386+
@pytest.mark.parametrize(
387+
"column_mapping",
388+
[
389+
{"query": "${foo.query}"},
390+
{"query": "${data.query"},
391+
{"query": "data.query", "response": "target.response"},
392+
{"query": "${data.query}", "response": "${target.response.one}"},
393+
]
394+
)
395+
def test_evaluate_invalid_column_mapping(self, mock_model_config, evaluate_test_data_jsonl_file, column_mapping):
387396
# Invalid source reference
388397
with pytest.raises(EvaluationException) as exc_info:
389398
evaluate(
390399
data=evaluate_test_data_jsonl_file,
391400
evaluators={"g": GroundednessEvaluator(model_config=mock_model_config)},
392-
evaluator_config={"g": {"column_mapping": {"query": "${foo.query}"}}},
401+
evaluator_config={
402+
"g": {
403+
"column_mapping": column_mapping,
404+
}
405+
}
393406
)
394407

395408
assert (
396-
"Unexpected references detected in 'column_mapping'. Ensure only ${target.} and ${data.} are used."
397-
in exc_info.value.args[0]
409+
"Unexpected references detected in 'column_mapping'. Ensure only ${target.} and ${data.} are used."
410+
in exc_info.value.args[0]
398411
)
399412

400413
def test_renaming_column(self):

0 commit comments

Comments
 (0)