Add proper TypedDict types and TypeAdapter validation to datasets API client#1724
Add proper TypedDict types and TypeAdapter validation to datasets API client#1724
Conversation
… client Replace untyped dict[str, Any] return types with proper TypedDicts derived from backend endpoint definitions. Add Pydantic TypeAdapters to validate/coerce API responses (string UUIDs → UUID, string datetimes → datetime). Add CaseData TypedDict for add_cases parameter typing.
| class ExportedCase(TypedDict): | ||
| """A case in pydantic-evals compatible format, part of :class:`ExportedDataset`.""" | ||
|
|
||
| name: str | None | ||
| inputs: Any | ||
| metadata: Any | ||
| expected_output: Any | ||
| evaluators: list[EvaluatorSpec] | None |
There was a problem hiding this comment.
🚩 All ExportedCase fields are required (no NotRequired), which may be stricter than API
ExportedCase at logfire/experimental/api_client.py:173-180 makes all fields required (no NotRequired). If the backend ever omits optional fields like metadata or evaluators from the export response (rather than sending null), the TypeAdapter validation would fail with a ValidationError. The current test fixture FAKE_EXPORT_JSON includes all fields. This is worth verifying against the actual API contract — if the API can omit fields, some should use NotRequired.
Was this helpful? React with 👍 or 👎 to provide feedback.
|
This will make it hard to change the backend endpoints (which we're currently likely to do) without breaking clients. Users might be forced to upgrade their SDK. One option is to try to validate, and if it fails, emit a warning and return the raw dict. Another option looks like this: class _DatasetDetailDict(TypedDict):
...
class DatasetDetail:
raw_data: _DatasetDetailDict
@property
def name(self):
return self.raw_data["name"]
@property
def created_at(self):
return datetime.fromisoformat(self.raw_data["created_at"])Here |
Summary
dict[str, Any]return types inlogfire/experimental/api_client.pywith proper TypedDicts (DatasetSummary,DatasetDetail,CaseDetail,ExportedCase,ExportedDataset,EvaluatorSpec) derived from backend endpoint definitionsTypeAdapters to validate/coerce API responses, converting string UUIDs and datetimes to properUUIDanddatetimePython typesCaseDataTypedDict for theadd_casesparameter (replacingSequence[dict[str, Any]])Anyforinputs/expected_output/metadatafields to support pydantic-evals non-dict values (e.g.,Case[str, str, None])datasets/__init__.pyexports and test fixturesTest plan
test_datasets_client.pytest_logfire_api.py