Skip to content

Commit afa17d0

Browse files
sararobcopybara-github
authored andcommitted
chore: Resolve all evals mypy errors
PiperOrigin-RevId: 862800470
1 parent 66c4d85 commit afa17d0

File tree

3 files changed

+47
-37
lines changed

3 files changed

+47
-37
lines changed

vertexai/_genai/_evals_common.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171

7272

7373
@contextlib.contextmanager
74-
def _temp_logger_level(logger_name: str, level: int):
74+
def _temp_logger_level(logger_name: str, level: int) -> None: # type: ignore[misc]
7575
"""Temporarily sets the level of a logger."""
7676
logger_instance = logging.getLogger(logger_name)
7777
original_level = logger_instance.getEffectiveLevel()
@@ -95,7 +95,7 @@ def _get_api_client_with_location(
9595
location,
9696
api_client.location,
9797
)
98-
return vertexai.Client(
98+
return vertexai.Client( # type: ignore[no-any-return]
9999
project=api_client.project,
100100
location=location,
101101
credentials=api_client._credentials,
@@ -1798,10 +1798,10 @@ def _convert_evaluation_run_results(
17981798
api_client: BaseApiClient,
17991799
evaluation_run_results: types.EvaluationRunResults,
18001800
inference_configs: Optional[dict[str, types.EvaluationRunInferenceConfig]] = None,
1801-
) -> Union[list[types.EvaluationItem], types.EvaluationResult]:
1801+
) -> Optional[types.EvaluationResult]:
18021802
"""Retrieves an EvaluationItem from the EvaluationRunResults."""
18031803
if not evaluation_run_results or not evaluation_run_results.evaluation_set:
1804-
return []
1804+
return None
18051805

18061806
evals_module = evals.Evals(api_client_=api_client)
18071807
eval_set = evals_module.get_evaluation_set(
@@ -1823,10 +1823,10 @@ async def _convert_evaluation_run_results_async(
18231823
api_client: BaseApiClient,
18241824
evaluation_run_results: types.EvaluationRunResults,
18251825
inference_configs: Optional[dict[str, types.EvaluationRunInferenceConfig]] = None,
1826-
) -> Union[list[types.EvaluationItem], types.EvaluationResult]:
1826+
) -> Optional[types.EvaluationResult]:
18271827
"""Retrieves an EvaluationItem from the EvaluationRunResults."""
18281828
if not evaluation_run_results or not evaluation_run_results.evaluation_set:
1829-
return []
1829+
return None
18301830

18311831
evals_module = evals.AsyncEvals(api_client_=api_client)
18321832
eval_set = await evals_module.get_evaluation_set(

vertexai/_genai/_evals_metric_handlers.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -617,15 +617,15 @@ def _build_pointwise_input(
617617

618618
def _add_autorater_config(self, payload: dict[str, Any]) -> None:
619619
"""Adds autorater config to the request payload if specified."""
620-
autorater_config = {}
620+
autorater_config: dict[str, Any] = {}
621621
if self.metric.judge_model:
622622
autorater_config["autorater_model"] = self.metric.judge_model
623623
if self.metric.judge_model_generation_config:
624624
autorater_config["generation_config"] = (
625625
self.metric.judge_model_generation_config
626626
)
627627
if self.metric.judge_model_sampling_count:
628-
autorater_config["sampling_count"] = self.metric.judge_model_sampling_count # type: ignore[assignment]
628+
autorater_config["sampling_count"] = self.metric.judge_model_sampling_count
629629

630630
if not autorater_config:
631631
return
@@ -989,11 +989,11 @@ def _build_request_payload(
989989
agent_data=PredefinedMetricHandler._eval_case_to_agent_data(eval_case),
990990
)
991991

992-
request_payload = {
992+
request_payload: dict[str, Any] = {
993993
"instance": instance_payload,
994994
}
995995

996-
autorater_config = {}
996+
autorater_config: dict[str, Any] = {}
997997
if self.metric.judge_model:
998998
autorater_config["autorater_model"] = self.metric.judge_model
999999
if self.metric.judge_model_generation_config:

vertexai/_genai/evals.py

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
try:
3737
from google.adk.agents import LlmAgent
3838
except ImportError:
39-
LlmAgent = None # type: ignore[assignment]
39+
LlmAgent = None
4040

4141

4242
logger = logging.getLogger("vertexai_genai.evals")
@@ -1216,10 +1216,10 @@ def evaluate(
12161216
types.EvaluationDatasetOrDict,
12171217
list[types.EvaluationDatasetOrDict],
12181218
],
1219-
metrics: list[types.MetricOrDict] = None,
1219+
metrics: Optional[list[types.MetricOrDict]] = None,
12201220
location: Optional[str] = None,
12211221
config: Optional[types.EvaluateMethodConfigOrDict] = None,
1222-
**kwargs,
1222+
**kwargs: Any,
12231223
) -> types.EvaluationResult:
12241224
"""Evaluates candidate responses in the provided dataset(s) using the specified metrics.
12251225
@@ -1625,24 +1625,28 @@ def create_evaluation_run(
16251625
raise ValueError(
16261626
"At most one of agent_info or inference_configs can be provided."
16271627
)
1628-
if agent_info and isinstance(agent_info, dict):
1629-
agent_info = types.evals.AgentInfo.model_validate(agent_info)
1630-
if type(dataset).__name__ == "EvaluationDataset":
1628+
agent_info_pydantic: types.evals.AgentInfo = types.evals.AgentInfo()
1629+
if agent_info:
1630+
if isinstance(agent_info, dict):
1631+
agent_info_pydantic = types.evals.AgentInfo.model_validate(agent_info)
1632+
else:
1633+
agent_info_pydantic = agent_info
1634+
if isinstance(dataset, types.EvaluationDataset):
16311635
if dataset.eval_dataset_df is None:
16321636
raise ValueError(
16331637
"EvaluationDataset must have eval_dataset_df populated."
16341638
)
1635-
if (
1639+
if agent_info_pydantic is not None and (
16361640
dataset.candidate_name
1637-
and agent_info
1638-
and agent_info.name
1639-
and dataset.candidate_name != agent_info.name
1641+
and agent_info_pydantic
1642+
and agent_info_pydantic.name
1643+
and dataset.candidate_name != agent_info_pydantic.name
16401644
):
16411645
logger.warning(
16421646
"Evaluation dataset candidate_name and agent_info.name are different. Please make sure this is intended."
16431647
)
1644-
elif dataset.candidate_name is None and agent_info:
1645-
dataset.candidate_name = agent_info.name
1648+
elif dataset.candidate_name is None and agent_info_pydantic:
1649+
dataset.candidate_name = agent_info_pydantic.name
16461650
eval_set = _evals_common._create_evaluation_set_from_dataframe(
16471651
self._api_client, dest, dataset.eval_dataset_df, dataset.candidate_name
16481652
)
@@ -1656,20 +1660,26 @@ def create_evaluation_run(
16561660
evaluation_config = types.EvaluationRunConfig(
16571661
output_config=output_config, metrics=resolved_metrics
16581662
)
1659-
if agent_info:
1663+
if agent_info_pydantic and agent_info_pydantic.name is not None:
16601664
inference_configs = {}
1661-
inference_configs[agent_info.name] = types.EvaluationRunInferenceConfig(
1662-
agent_config=types.EvaluationRunAgentConfig(
1663-
developer_instruction=genai_types.Content(
1664-
parts=[genai_types.Part(text=agent_info.instruction)]
1665-
),
1666-
tools=agent_info.tool_declarations,
1665+
inference_configs[agent_info_pydantic.name] = (
1666+
types.EvaluationRunInferenceConfig(
1667+
agent_config=types.EvaluationRunAgentConfig(
1668+
developer_instruction=genai_types.Content(
1669+
parts=[
1670+
genai_types.Part(text=agent_info_pydantic.instruction)
1671+
]
1672+
),
1673+
tools=agent_info_pydantic.tool_declarations,
1674+
)
16671675
)
16681676
)
1669-
if agent_info.agent_resource_name:
1677+
if agent_info_pydantic.agent_resource_name:
16701678
labels = labels or {}
16711679
labels["vertex-ai-evaluation-agent-engine-id"] = (
1672-
agent_info.agent_resource_name.split("reasoningEngines/")[-1]
1680+
agent_info_pydantic.agent_resource_name.split("reasoningEngines/")[
1681+
-1
1682+
]
16731683
)
16741684
if not name:
16751685
name = f"evaluation_run_{uuid.uuid4()}"
@@ -2487,12 +2497,12 @@ async def create_evaluation_run(
24872497
)
24882498
if agent_info and isinstance(agent_info, dict):
24892499
agent_info = types.evals.AgentInfo.model_validate(agent_info)
2490-
if type(dataset).__name__ == "EvaluationDataset":
2500+
if isinstance(dataset, types.EvaluationDataset):
24912501
if dataset.eval_dataset_df is None:
24922502
raise ValueError(
24932503
"EvaluationDataset must have eval_dataset_df populated."
24942504
)
2495-
if (
2505+
if agent_info is not None and (
24962506
dataset.candidate_name
24972507
and agent_info.name
24982508
and dataset.candidate_name != agent_info.name
@@ -2515,7 +2525,7 @@ async def create_evaluation_run(
25152525
evaluation_config = types.EvaluationRunConfig(
25162526
output_config=output_config, metrics=resolved_metrics
25172527
)
2518-
if agent_info:
2528+
if agent_info and agent_info.name is not None:
25192529
inference_configs = {}
25202530
inference_configs[agent_info.name] = types.EvaluationRunInferenceConfig(
25212531
agent_config=types.EvaluationRunAgentConfig(
@@ -2533,7 +2543,7 @@ async def create_evaluation_run(
25332543
if not name:
25342544
name = f"evaluation_run_{uuid.uuid4()}"
25352545

2536-
result = await self._create_evaluation_run( # type: ignore[no-any-return]
2546+
result = await self._create_evaluation_run(
25372547
name=name,
25382548
display_name=display_name or name,
25392549
data_source=dataset,
@@ -2645,7 +2655,7 @@ async def create_evaluation_item(
26452655
Returns:
26462656
The evaluation item.
26472657
"""
2648-
result = await self._create_evaluation_item( # type: ignore[no-any-return]
2658+
result = await self._create_evaluation_item(
26492659
evaluation_item_type=evaluation_item_type,
26502660
gcs_uri=gcs_uri,
26512661
display_name=display_name,
@@ -2676,7 +2686,7 @@ async def create_evaluation_set(
26762686
Returns:
26772687
The evaluation set.
26782688
"""
2679-
result = await self._create_evaluation_set( # type: ignore[no-any-return]
2689+
result = await self._create_evaluation_set(
26802690
evaluation_items=evaluation_items,
26812691
display_name=display_name,
26822692
config=config,

0 commit comments

Comments
 (0)