Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /PascalNobereit_langserve /tests /unit_tests /test_serialization.py
| import datetime | |
| import uuid | |
| from enum import Enum | |
| from typing import Any | |
| import pytest | |
| from langchain_core.messages import HumanMessage, HumanMessageChunk, SystemMessage | |
| from langchain_core.outputs import ChatGeneration | |
| try: | |
| from pydantic.v1 import BaseModel | |
| except ImportError: | |
| from pydantic import BaseModel | |
| from langserve.serialization import WellKnownLCSerializer, load_events | |
| def test_serialization(data: Any) -> None: | |
| """There and back again! :)""" | |
| # Test encoding | |
| lc_serializer = WellKnownLCSerializer() | |
| assert isinstance(lc_serializer.dumps(data), bytes) | |
| # Translate to python primitives and load back into object | |
| assert lc_serializer.loadd(lc_serializer.dumpd(data)) == data | |
| # Test simple equality (does not include pydantic class names) | |
| assert lc_serializer.loads(lc_serializer.dumps(data)) == data | |
| # Test full representation equality (includes pydantic class names) | |
| assert _get_full_representation( | |
| lc_serializer.loads(lc_serializer.dumps(data)) | |
| ) == _get_full_representation(data) | |
| def _get_full_representation(data: Any) -> Any: | |
| """Get the full representation of the data, replacing pydantic models with schema. | |
| Pydantic tests two different models for equality based on equality | |
| of their schema; instead we will rely on the equality of their full | |
| schema representation. This will make sure that both models have the | |
| same name (e.g., HumanMessage vs. HumanMessageChunk). | |
| Args: | |
| data: python primitives + pydantic models | |
| Returns: | |
| data represented entirely with python primitives | |
| """ | |
| if isinstance(data, dict): | |
| return {key: _get_full_representation(value) for key, value in data.items()} | |
| elif isinstance(data, list): | |
| return [_get_full_representation(value) for value in data] | |
| elif isinstance(data, BaseModel): | |
| return data.schema() | |
| else: | |
| return data | |
| def test_decode_events(data: Any, expected: Any) -> None: | |
| """Test decoding events.""" | |
| assert load_events(data) == expected | |
| class SimpleEnum(Enum): | |
| a = "a" | |
| b = "b" | |
| class SimpleModel(BaseModel): | |
| x: int | |
| y: SimpleEnum | |
| z: uuid.UUID | |
| dt: datetime.datetime | |
| d: datetime.date | |
| t: datetime.time | |
| def test_encoding_of_well_known_types(obj: Any, expected: str) -> None: | |
| """Test encoding of well known types. | |
| This tests verifies that our custom serializer is able to encode some well | |
| known types; e.g., uuid, datetime, date, time | |
| It doesn't handle types like Decimal or frozenset etc just yet while we determine | |
| how to roll out a more complete solution. | |
| """ | |
| lc_serializer = WellKnownLCSerializer() | |
| assert lc_serializer.dumpd(obj) == expected | |
Xet Storage Details
- Size:
- 5.71 kB
- Xet hash:
- 900320c6266a547198089e8febbb24c7ff08c6e981a9abf92d8f6d7a0eea81c2
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.