Skip to content

Conversation

@eramis73
Copy link

This PR adds Google-style docstrings to public metrics in dspy/evaluate/metrics.py.

  • Ensures correctness and clarity
  • Adds small usage examples
  • Passes pre-commit hooks

resolve #8953
cc @chenmoneygithub

@chenmoneygithub chenmoneygithub self-requested a review October 20, 2025 17:32
Copy link
Collaborator

@chenmoneygithub chenmoneygithub left a comment

Choose a reason for hiding this comment

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

Thanks for the PR!


def EM(prediction, answers_list): # noqa: N802
assert isinstance(answers_list, list)
"""Return True if any reference exactly matches the prediction (after normalization).
Copy link
Collaborator

Choose a reason for hiding this comment

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

The opening line should describe what this API is, instead of the API's behavior.

otherwise False.
Example:
>>> EM("The Eiffel Tower", ["Eiffel Tower", "Louvre"])
Copy link
Collaborator

Choose a reason for hiding this comment

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

This doesn't render on mkdocs, let's use the block style, e.g.,:

my_code

>>> EM("paris", ["Paris"])
True
"""
assert isinstance(answers_list, list)
Copy link
Collaborator

Choose a reason for hiding this comment

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

let's don't mix fix with docstring changes. Actually this assert statement won't provide more information to users

>>> round(F1("Eiffel Tower is in Paris", ["Paris"]), 2)
0.33
"""
assert isinstance(answers_list, list)
Copy link
Collaborator

Choose a reason for hiding this comment

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

ditto

float: The highest HotpotQA-style F1 score in [0.0, 1.0].
Example:
>>> HotPotF1("yes", ["no"])
Copy link
Collaborator

Choose a reason for hiding this comment

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

use block code


def remove_articles(text):
return re.sub(r"\b(a|an|the)\b", " ", text)
return re.sub(r"\\b(a|an|the)\\b", " ", text)
Copy link
Collaborator

Choose a reason for hiding this comment

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

do we need to change this?

def answer_exact_match(example, pred, trace=None, frac=1.0):
"""Example/Prediction evaluator for answer strings with EM/F1 thresholding.
If ``example.answer`` is a string, compare ``pred.answer`` against it.
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: single backtick around variables: example.answer



def answer_exact_match(example, pred, trace=None, frac=1.0):
"""Example/Prediction evaluator for answer strings with EM/F1 thresholding.
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is too detailed for the open sentence

@eramis73
Copy link
Author

All feedback addressed (concise openings + mkdocs block examples)
Non-doc changes reverted
Ready for re-review. Thanks @chenmoneygithub!

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.

[docs] Add Google-style docstrings for dspy/evaluate/metrics.py

2 participants