{"id":9481,"library":"apischema","title":"apischema: JSON (de)serialization, GraphQL, and JSON Schema","description":"apischema is a Python library for JSON (de)serialization, GraphQL, and JSON schema generation, heavily leveraging Python's type hints. It is currently at version 0.19.0 and maintains an active release cadence with frequent updates and bug fixes.","status":"active","version":"0.19.0","language":"en","source_language":"en","source_url":"https://github.com/wyfo/apischema","tags":["json","serialization","deserialization","graphql","json-schema","typing","dataclasses","validation"],"install":[{"cmd":"pip install apischema","lang":"bash","label":"Install core library"},{"cmd":"pip install apischema[graphql]","lang":"bash","label":"Install with GraphQL support"},{"cmd":"pip install apischema[validation]","lang":"bash","label":"Install with pydantic for validation"}],"dependencies":[{"reason":"Required for Python versions < 3.11 for certain typing features.","package":"typing-extensions","optional":false}],"imports":[{"symbol":"serialize","correct":"from apischema import serialize"},{"symbol":"deserialize","correct":"from apischema import deserialize"},{"symbol":"schema","correct":"from apischema import schema"},{"note":"The direct `schema_json` import was deprecated/removed; use `schema_json_from_obj` from `apischema.json_schema` for clarity and consistency.","wrong":"from apischema import schema_json","symbol":"schema_json_from_obj","correct":"from apischema.json_schema import schema_json_from_obj"},{"symbol":"ValidationError","correct":"from apischema.validation.errors import ValidationError"},{"note":"While standard `dataclasses.dataclass` works, `apischema.dataclasses.dataclass` provides enhanced features and integration with apischema.","wrong":"from dataclasses import dataclass # (potentially less features)","symbol":"dataclass","correct":"from apischema.dataclasses import dataclass"}],"quickstart":{"code":"from dataclasses import dataclass\nfrom apischema import serialize, deserialize\nfrom apischema.validation.errors import ValidationError\n\n@dataclass\nclass User:\n    id: int\n    name: str\n    email: str | None = None\n\n# Serialization\nuser_obj = User(id=1, name=\"Alice\", email=\"alice@example.com\")\nserialized_data = serialize(user_obj)\nprint(f\"Serialized: {serialized_data}\")\n# Expected output: {'id': 1, 'name': 'Alice', 'email': 'alice@example.com'}\n\n# Deserialization\ndeserialized_obj = deserialize(User, {'id': 2, 'name': 'Bob'})\nprint(f\"Deserialized: {deserialized_obj}\")\n# Expected output: User(id=2, name='Bob', email=None)\n\n# Handling validation errors\ntry:\n    deserialize(User, {'id': 'not-an-int', 'name': 'Charlie'})\nexcept ValidationError as e:\n    print(f\"Validation Error: {e}\")\n# Expected output: Validation Error: [id]: must be an integer\n","lang":"python","description":"This quickstart demonstrates basic serialization and deserialization of a Python dataclass using `apischema`, including an example of how to handle `ValidationError`."},"warnings":[{"fix":"Upgrade Python to 3.9 or newer. Consider using a virtual environment.","message":"Python 3.7 support was dropped in v0.18.2. Users on Python 3.7 or older must upgrade their Python environment to at least 3.9 (as per current PyPI `requires_python`) to use recent versions of apischema.","severity":"breaking","affected_versions":">=0.18.2"},{"fix":"Consult the `apischema` changelog for v0.18.0 and upgrade to the new API. Specifically look for changes around `schema_json` and `confs`.","message":"Various deprecated APIs were removed in v0.18.0. Code relying on functions or classes marked as deprecated in earlier versions will break.","severity":"breaking","affected_versions":">=0.18.0"},{"fix":"Update error handling logic for custom serializers. If you were catching `ValidationError` for all exceptions during serialization, you might now need to catch more specific exception types or `Exception` directly.","message":"Since v0.17.0, arbitrary exceptions raised during serialization are no longer automatically converted to `ValidationError`. This change enhances security by preventing unexpected exception type conversion.","severity":"breaking","affected_versions":">=0.17.0"},{"fix":"Import `dataclass` from `apischema.dataclasses` to leverage advanced `apischema` features like `with_args` or special metadata handling.","message":"When defining dataclasses, using `apischema.dataclasses.dataclass` is often preferred over `dataclasses.dataclass` (from the standard library) for full feature integration.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-17T00:00:00.000Z","next_check":"2026-07-16T00:00:00.000Z","problems":[{"fix":"Ensure your Python version is 3.9 or newer. If on Python 3.8, ensure `typing-extensions` is installed and updated (`pip install 'typing-extensions>=4.0'`). Alternatively, use `typing.List[str]` syntax for older Python versions.","cause":"Using type hints like `list[str]` or `dict[str, int]` on Python versions older than 3.9, or when `typing-extensions` is not installed on Python 3.8 where it might be needed.","error":"TypeError: 'type' object is not subscriptable"},{"fix":"Examine the error message to identify the field (`[path]`) and the expected type/format. Adjust the input data to match the Python type definition, or refine the type definition to correctly represent the expected data.","cause":"Input data does not conform to the type annotations or validation rules defined in the Python type. This is `apischema`'s way of reporting schema mismatches.","error":"apischema.validation.errors.ValidationError: [path]: error message"},{"fix":"Change your import statement from `from apischema import schema_json` to `from apischema.json_schema import schema_json_from_obj`.","cause":"The `schema_json` function was relocated to `apischema.json_schema.schema_json_from_obj` in newer versions.","error":"ImportError: cannot import name 'schema_json' from 'apischema'"},{"fix":"If configuring global settings, use `apischema.confs.set_deserialization_coercion(True)` or similar methods, or directly modify attributes like `apischema.settings.deserialization.coercion = True`. Do not treat `apischema.settings` as a function.","cause":"In older versions, `apischema.settings` was sometimes used as a callable. In newer versions (e.g., v0.18.0+), `apischema.settings` is an object, and configuration is done through `apischema.confs` or by directly setting attributes on the `apischema.settings` object.","error":"TypeError: 'ApischemaSettings' object is not callable"}]}