Skip to content

Commit 324e730

Browse files
sararobcopybara-github
authored andcommitted
chore: Resolve all evals mypy errors
PiperOrigin-RevId: 862800470
1 parent d685d81 commit 324e730

3 files changed

Lines changed: 43 additions & 36 deletions

File tree

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: 33 additions & 26 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,25 @@ def create_evaluation_run(
16251625
raise ValueError(
16261626
"At most one of agent_info or inference_configs can be provided."
16271627
)
1628+
agent_info_pydantic: types.evals.AgentInfo = types.evals.AgentInfo()
16281629
if agent_info and isinstance(agent_info, dict):
1629-
agent_info = types.evals.AgentInfo.model_validate(agent_info)
1630-
if type(dataset).__name__ == "EvaluationDataset":
1630+
agent_info_pydantic = types.evals.AgentInfo.model_validate(agent_info)
1631+
if isinstance(dataset, types.EvaluationDataset):
16311632
if dataset.eval_dataset_df is None:
16321633
raise ValueError(
16331634
"EvaluationDataset must have eval_dataset_df populated."
16341635
)
1635-
if (
1636+
if agent_info_pydantic is not None and (
16361637
dataset.candidate_name
1637-
and agent_info
1638-
and agent_info.name
1639-
and dataset.candidate_name != agent_info.name
1638+
and agent_info_pydantic
1639+
and agent_info_pydantic.name
1640+
and dataset.candidate_name != agent_info_pydantic.name
16401641
):
16411642
logger.warning(
16421643
"Evaluation dataset candidate_name and agent_info.name are different. Please make sure this is intended."
16431644
)
1644-
elif dataset.candidate_name is None and agent_info:
1645-
dataset.candidate_name = agent_info.name
1645+
elif dataset.candidate_name is None and agent_info_pydantic:
1646+
dataset.candidate_name = agent_info_pydantic.name
16461647
eval_set = _evals_common._create_evaluation_set_from_dataframe(
16471648
self._api_client, dest, dataset.eval_dataset_df, dataset.candidate_name
16481649
)
@@ -1656,20 +1657,26 @@ def create_evaluation_run(
16561657
evaluation_config = types.EvaluationRunConfig(
16571658
output_config=output_config, metrics=resolved_metrics
16581659
)
1659-
if agent_info:
1660+
if agent_info_pydantic and agent_info_pydantic.name is not None:
16601661
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,
1662+
inference_configs[agent_info_pydantic.name] = (
1663+
types.EvaluationRunInferenceConfig(
1664+
agent_config=types.EvaluationRunAgentConfig(
1665+
developer_instruction=genai_types.Content(
1666+
parts=[
1667+
genai_types.Part(text=agent_info_pydantic.instruction)
1668+
]
1669+
),
1670+
tools=agent_info_pydantic.tool_declarations,
1671+
)
16671672
)
16681673
)
1669-
if agent_info.agent_resource_name:
1674+
if agent_info_pydantic.agent_resource_name:
16701675
labels = labels or {}
16711676
labels["vertex-ai-evaluation-agent-engine-id"] = (
1672-
agent_info.agent_resource_name.split("reasoningEngines/")[-1]
1677+
agent_info_pydantic.agent_resource_name.split("reasoningEngines/")[
1678+
-1
1679+
]
16731680
)
16741681
if not name:
16751682
name = f"evaluation_run_{uuid.uuid4()}"
@@ -2487,12 +2494,12 @@ async def create_evaluation_run(
24872494
)
24882495
if agent_info and isinstance(agent_info, dict):
24892496
agent_info = types.evals.AgentInfo.model_validate(agent_info)
2490-
if type(dataset).__name__ == "EvaluationDataset":
2497+
if isinstance(dataset, types.EvaluationDataset):
24912498
if dataset.eval_dataset_df is None:
24922499
raise ValueError(
24932500
"EvaluationDataset must have eval_dataset_df populated."
24942501
)
2495-
if (
2502+
if agent_info is not None and (
24962503
dataset.candidate_name
24972504
and agent_info.name
24982505
and dataset.candidate_name != agent_info.name
@@ -2515,7 +2522,7 @@ async def create_evaluation_run(
25152522
evaluation_config = types.EvaluationRunConfig(
25162523
output_config=output_config, metrics=resolved_metrics
25172524
)
2518-
if agent_info:
2525+
if agent_info and agent_info.name is not None:
25192526
inference_configs = {}
25202527
inference_configs[agent_info.name] = types.EvaluationRunInferenceConfig(
25212528
agent_config=types.EvaluationRunAgentConfig(
@@ -2533,7 +2540,7 @@ async def create_evaluation_run(
25332540
if not name:
25342541
name = f"evaluation_run_{uuid.uuid4()}"
25352542

2536-
result = await self._create_evaluation_run( # type: ignore[no-any-return]
2543+
result = await self._create_evaluation_run(
25372544
name=name,
25382545
display_name=display_name or name,
25392546
data_source=dataset,
@@ -2645,7 +2652,7 @@ async def create_evaluation_item(
26452652
Returns:
26462653
The evaluation item.
26472654
"""
2648-
result = await self._create_evaluation_item( # type: ignore[no-any-return]
2655+
result = await self._create_evaluation_item(
26492656
evaluation_item_type=evaluation_item_type,
26502657
gcs_uri=gcs_uri,
26512658
display_name=display_name,
@@ -2676,7 +2683,7 @@ async def create_evaluation_set(
26762683
Returns:
26772684
The evaluation set.
26782685
"""
2679-
result = await self._create_evaluation_set( # type: ignore[no-any-return]
2686+
result = await self._create_evaluation_set(
26802687
evaluation_items=evaluation_items,
26812688
display_name=display_name,
26822689
config=config,

0 commit comments

Comments
 (0)