Enhancement: Add type validation for cost_metadata parameter in CostTracker.record_usage #10572

Open
opened 2026-04-18 17:26:34 +00:00 by HAL9000 · 0 comments
Owner

Metadata

  • Commit: HEAD
  • Branch: main

Background and Context

In src/cleveragents/providers/cost_tracker.py, the record_usage() method accepts a cost_metadata parameter but doesn't validate its type at runtime. While the type hint indicates that cost_metadata should be a CostMetadata instance, there's no runtime validation. If a caller passes an object that doesn't have the required methods/attributes (e.g., a dict or None), the method will crash with an AttributeError instead of a clear TypeError.

This is a type safety issue that should be addressed to provide better error messages and fail fast with clear feedback to callers.

Expected Behavior

The record_usage() method should validate that the cost_metadata parameter is an instance of CostMetadata and raise a clear TypeError with a descriptive message if not.

Actual Behavior

The method crashes with AttributeError: 'dict' object has no attribute 'record_usage' when cost_metadata is not a CostMetadata instance, providing unclear error messages to callers.

Acceptance Criteria

  • The record_usage() method validates that cost_metadata is an instance of CostMetadata
  • A clear TypeError is raised with a descriptive message if validation fails
  • Unit tests verify the validation behavior for invalid input types (dict, None, string, etc.)
  • The validation occurs at the start of the method before any attribute access

Subtasks

  • Add type validation logic to CostTracker.record_usage() method
  • Create unit tests for invalid cost_metadata parameter types
  • Update method documentation if needed
  • Verify no existing code passes invalid types to this method

Definition of Done

  • Type validation is implemented and raises clear TypeError for invalid inputs
  • All unit tests pass, including new validation tests
  • Code review approved
  • No regressions in existing functionality

Automated by CleverAgents Bot
Agent: new-issue-creator

## Metadata - **Commit:** HEAD - **Branch:** main ## Background and Context In `src/cleveragents/providers/cost_tracker.py`, the `record_usage()` method accepts a `cost_metadata` parameter but doesn't validate its type at runtime. While the type hint indicates that `cost_metadata` should be a `CostMetadata` instance, there's no runtime validation. If a caller passes an object that doesn't have the required methods/attributes (e.g., a dict or None), the method will crash with an AttributeError instead of a clear TypeError. This is a type safety issue that should be addressed to provide better error messages and fail fast with clear feedback to callers. ## Expected Behavior The `record_usage()` method should validate that the `cost_metadata` parameter is an instance of `CostMetadata` and raise a clear `TypeError` with a descriptive message if not. ## Actual Behavior The method crashes with `AttributeError: 'dict' object has no attribute 'record_usage'` when `cost_metadata` is not a `CostMetadata` instance, providing unclear error messages to callers. ## Acceptance Criteria - [ ] The `record_usage()` method validates that `cost_metadata` is an instance of `CostMetadata` - [ ] A clear `TypeError` is raised with a descriptive message if validation fails - [ ] Unit tests verify the validation behavior for invalid input types (dict, None, string, etc.) - [ ] The validation occurs at the start of the method before any attribute access ## Subtasks - [ ] Add type validation logic to `CostTracker.record_usage()` method - [ ] Create unit tests for invalid `cost_metadata` parameter types - [ ] Update method documentation if needed - [ ] Verify no existing code passes invalid types to this method ## Definition of Done - Type validation is implemented and raises clear TypeError for invalid inputs - All unit tests pass, including new validation tests - Code review approved - No regressions in existing functionality --- **Automated by CleverAgents Bot** Agent: new-issue-creator
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
cleveragents/cleveragents-core#10572
No description provided.