Skip to content

Conversation

@adivate2021
Copy link
Contributor

No description provided.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @adivate2021, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly extends the judgeval library's capabilities by introducing comprehensive support for custom trace scorers. It enables users to define, upload, and execute Python-based scorers that evaluate OpenTelemetry trace data, providing a powerful new way to assess the performance and behavior of systems based on their execution traces. The changes include new data models for traces, a dedicated base class for trace scorers, and the necessary infrastructure for asynchronous scoring and result handling, all integrated seamlessly into the existing custom scorer upload mechanism.

Highlights

  • TraceScorer Introduction: Introduced a new TraceScorer base class and the necessary infrastructure for evaluating OpenTelemetry trace data, allowing for custom scoring logic on execution traces.
  • Unified Scorer Upload Mechanism: The upload_custom_scorer function and its corresponding CLI command now support uploading both ExampleScorer and the new TraceScorer types, distinguished by an is_trace flag.
  • New Trace Data Models: Added TraceData and TraceSpanData models to represent and handle OpenTelemetry traces within the system, enabling structured processing of trace information.
  • Asynchronous Trace Scoring Infrastructure: Implemented new asynchronous functions (a_execute_trace_scoring, safe_a_score_trace) for efficient, concurrent evaluation of traces with custom scorers, including robust error handling and progress reporting.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces support for custom trace scorers, a significant feature enhancement. The changes correctly propagate the is_trace flag from the CLI and client methods down to the API payload, and new data models for traces are introduced. The core logic for executing trace scoring is well-structured but has some areas for improvement. My review focuses on improving the robustness, maintainability, and documentation of the new trace scoring implementation in src/judgeval/scorers/score_trace.py.

Comment on lines +187 to +190
scoring_result = generate_scoring_result(
trace.trace_spans[0], scorer_data_list, run_duration, success
)
scoring_results[score_index] = scoring_result
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Accessing trace.trace_spans[0] assumes that the trace_spans list is never empty. If a TraceData object is passed with an empty trace_spans list, this will raise an IndexError and crash the scoring process for that trace. It's crucial to add a check to handle this edge case gracefully to prevent runtime errors.

Suggested change
scoring_result = generate_scoring_result(
trace.trace_spans[0], scorer_data_list, run_duration, success
)
scoring_results[score_index] = scoring_result
if not trace.trace_spans:
judgeval_logger.warning("Trace contains no spans. Skipping result generation for this trace.")
return
scoring_result = generate_scoring_result(
trace.trace_spans[0], scorer_data_list, run_duration, success
)
scoring_results[score_index] = scoring_result

Comment on lines +30 to +31
scorer (TraceScorer): The `TraceScorer` to use for scoring the trace.
trace (Trace): The `Trace` to be scored.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The type hint for the trace parameter in the docstring is Trace, but the function signature specifies TraceData. To avoid confusion and ensure documentation accuracy, the docstring should be updated to match the signature.

Suggested change
scorer (TraceScorer): The `TraceScorer` to use for scoring the trace.
trace (Trace): The `Trace` to be scored.
scorer (TraceScorer): The `TraceScorer` to use for scoring the trace.
trace (TraceData): The `TraceData` to be scored.

Each `Trace` will be evaluated by all of the `TraceScorer`s in the `scorers` list.
Args:
traces (List[List[TraceSpan]]): A list of `TraceSpan` objects to be evaluated.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The type hint for the traces parameter in the docstring is List[List[TraceSpan]], but the function signature uses List[TraceData]. This is inconsistent and could be misleading for developers using this function. Please update the docstring to match the function signature.

Suggested change
traces (List[List[TraceSpan]]): A list of `TraceSpan` objects to be evaluated.
traces (List[TraceData]): A list of `TraceData` objects to be evaluated.

Comment on lines +97 to +143
if show_progress:
with tqdm_asyncio(
desc=f"Evaluating {len(traces)} trace(s) in parallel",
unit="TraceData",
total=len(traces),
bar_format="{desc}: |{bar}|{percentage:3.0f}% ({n_fmt}/{total_fmt}) [Time Taken: {elapsed}, {rate_fmt}{postfix}]",
) as pbar:
for i, trace in enumerate(traces):
if isinstance(trace, TraceData):
if len(scorers) == 0:
pbar.update(1)
continue

cloned_scorers = clone_scorers(scorers) # type: ignore
task = execute_with_semaphore(
func=a_eval_traces_helper,
scorers=cloned_scorers,
trace=trace,
scoring_results=scoring_results,
score_index=i,
ignore_errors=ignore_errors,
pbar=pbar,
)
tasks.append(asyncio.create_task(task))

await asyncio.sleep(throttle_value)
await asyncio.gather(*tasks)
else:
for i, trace in enumerate(traces):
if isinstance(trace, TraceData):
if len(scorers) == 0:
continue

cloned_scorers = clone_scorers(scorers) # type: ignore
task = execute_with_semaphore(
func=a_eval_traces_helper,
scorers=cloned_scorers,
trace=trace,
scoring_results=scoring_results,
score_index=i,
ignore_errors=ignore_errors,
pbar=None,
)
tasks.append(asyncio.create_task(task))

await asyncio.sleep(throttle_value)
await asyncio.gather(*tasks)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

There is significant code duplication between the if show_progress: and else: blocks. The core logic for iterating through traces, cloning scorers, and creating asynchronous tasks is identical in both branches. This duplication makes the code harder to maintain, as any change needs to be applied in two places.

To improve maintainability, consider refactoring this to a single loop. You could use a context manager for the progress bar that does nothing when show_progress is False, allowing you to unify the logic. For example:

import contextlib

# ...

progress_context = tqdm_asyncio(...) if show_progress else contextlib.nullcontext()
with progress_context as pbar:
    for i, trace in enumerate(traces):
        # ... common logic for creating tasks ...
        # pass pbar to helper, which can handle if it's None

Args:
scorers (List[TraceScorer]): List of TraceScorer objects to evaluate the trace.
trace (Trace): The trace to be evaluated.
scoring_results (List[TestResult]): List to store the scoring results.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The type hint for the scoring_results parameter in the docstring is List[TestResult]. This appears to be a copy-paste error, as the function signature correctly types it as List[ScoringResult]. Please correct the docstring to maintain consistency and avoid confusion.

Suggested change
scoring_results (List[TestResult]): List to store the scoring results.
scoring_results (List[ScoringResult]): List to store the scoring results.

@propel-code-bot
Copy link

✔️ Propel has finished reviewing this change.

return


async def a_execute_trace_scoring(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[BestPractice]

[CodeDuplication] The logic in this file for scoring traces is almost a complete duplicate of the logic for scoring examples in src/judgeval/scorers/score.py. The functions safe_a_score_trace, a_execute_trace_scoring, and a_eval_traces_helper are structurally identical to their counterparts in score.py (safe_a_score_example, a_execute_scoring, a_eval_examples_helper).

To avoid code duplication and improve maintainability, consider creating a generic scoring execution utility that can be reused for both ExampleScorer with Examples and TraceScorer with TraceData.

Context for Agents
[**BestPractice**]

[CodeDuplication] The logic in this file for scoring traces is almost a complete duplicate of the logic for scoring examples in `src/judgeval/scorers/score.py`. The functions `safe_a_score_trace`, `a_execute_trace_scoring`, and `a_eval_traces_helper` are structurally identical to their counterparts in `score.py` (`safe_a_score_example`, `a_execute_scoring`, `a_eval_examples_helper`).

To avoid code duplication and improve maintainability, consider creating a generic scoring execution utility that can be reused for both `ExampleScorer` with `Example`s and `TraceScorer` with `TraceData`.

File: src/judgeval/scorers/score_trace.py
Line: 55

Comment on lines +127 to 130
base_class_name = "TraceScorer" if is_trace else "ExampleScorer"

scorer_classes = []
for node in ast.walk(tree):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[BestPractice]

The logic to find the scorer class by walking the AST has been made more complex by the introduction of the is_trace flag. This section is becoming difficult to read and maintain. Consider extracting the AST parsing and validation logic into a dedicated helper function to improve clarity and separation of concerns.

Context for Agents
[**BestPractice**]

The logic to find the scorer class by walking the AST has been made more complex by the introduction of the `is_trace` flag. This section is becoming difficult to read and maintain. Consider extracting the AST parsing and validation logic into a dedicated helper function to improve clarity and separation of concerns.

File: src/judgeval/__init__.py
Line: 130

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants