Skip to content

Fix Score.as_dict() to include name field#1103

Merged
Alex Jean-Baptiste (justcodebruh) merged 1 commit intomainfrom
ajb-score-implentation-fix
Dec 17, 2025
Merged

Fix Score.as_dict() to include name field#1103
Alex Jean-Baptiste (justcodebruh) merged 1 commit intomainfrom
ajb-score-implentation-fix

Conversation

@justcodebruh
Copy link
Copy Markdown
Contributor

Summary

Fixes a bug in Score.as_dict() where the name field was missing when serializing, causing "Object of type Score is not JSON serializable" errors when returning Score objects from Python scorers.

Problem

The API expects Score objects to have name, score, and metadata fields, but as_dict() was only returning score and metadata. This caused the following error when using online scorers:

Cannot log [{"name":"...","score":1,"metadata":{...}},...] as a score
Object of type Score is not JSON serializable

Solution

Updated Score.as_dict() to include the name field:

def as_dict(self):
    return {
        "name": self.name,        # ← ADDED
        "score": self.score,
        "metadata": self.metadata,
    }

Changes

  • Updated py/src/braintrust/score.py: Fixed Score.as_dict() method
  • Added py/src/braintrust/test_score.py: Comprehensive test suite with 10 tests

Tests

All 10 new tests pass:

  • ✅ All required fields are included in serialization
  • ✅ JSON serialization works correctly
  • ✅ Round-trip serialization/deserialization
  • ✅ Arrays of Score objects serialize correctly
  • ✅ Edge cases (null scores, empty metadata, complex metadata)
  • ✅ Score validation bounds checking

🤖 Generated with Claude Code

The Score.as_dict() method was missing the 'name' field when serializing,
causing "Object of type Score is not JSON serializable" errors when
returning Score objects from Python scorers.

The API expects Score objects to have name, score, and metadata fields,
but as_dict() was only returning score and metadata.

Changes:
- Updated Score.as_dict() to include name field
- Added comprehensive test suite (test_score.py) with 10 tests covering:
  - All required fields are included
  - JSON serialization
  - Round-trip serialization/deserialization
  - Arrays of Score objects
  - Edge cases (null scores, empty metadata, complex metadata)
  - Score validation bounds

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
Copy link
Copy Markdown
Contributor

@realark Andrew Kent (realark) left a comment

Choose a reason for hiding this comment

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

LGTM -- reproduced this on a local playground and this patch fixed the issue with a python remote scorer

@justcodebruh Alex Jean-Baptiste (justcodebruh) merged commit 38ea029 into main Dec 17, 2025
47 checks passed
@justcodebruh Alex Jean-Baptiste (justcodebruh) deleted the ajb-score-implentation-fix branch December 17, 2025 17:01
Alex Jean-Baptiste (justcodebruh) added a commit to braintrustdata/autoevals that referenced this pull request Dec 18, 2025
## Summary

Fixes a bug in `Score.as_dict()` where the `name` field was missing when
serializing, causing "Object of type Score is not JSON serializable"
errors when returning Score objects from Python scorers.

## Problem

The API expects Score objects to have `name`, `score`, and `metadata`
fields, but `as_dict()` was only returning `score` and `metadata`. This
caused the following error when using online scorers:

```
Cannot log [{"name":"...","score":1,"metadata":{...}},...] as a score
Object of type Score is not JSON serializable
```

## Solution

Updated `Score.as_dict()` to include the `name` field:

```python
def as_dict(self):
    return {
        "name": self.name,        # ← ADDED
        "score": self.score,
        "metadata": self.metadata,
    }
```

## Changes

- Updated `py/autoevals/score.py`: Fixed `Score.as_dict()` method

This matches the fix in braintrust-sdk (PR
braintrustdata/braintrust-sdk-javascript#1103) and ensures Score objects serialize
correctly with all required fields.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: Alex Jean-Baptiste Jr <alex.jean-baptiste@braintrustdata.com>
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