{"id":6659,"library":"graphene-pydantic","title":"Graphene Pydantic Integration","description":"A Pydantic integration for Graphene, currently at version 0.6.1. It provides utilities to automatically convert Pydantic `BaseModel`s into Graphene `ObjectType`s and `InputObjectType`s, streamlining GraphQL schema generation. The library sees active development with updates addressing Pydantic and Graphene version compatibility.","status":"active","version":"0.6.1","language":"python","source_language":"en","source_url":"https://github.com/graphql-python/graphene-pydantic","tags":["graphene","pydantic","graphql","schema-generation","api"],"install":[{"cmd":"pip install \"graphene-pydantic\"","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Core dependency for GraphQL schema definition.","package":"graphene","optional":false},{"reason":"Core dependency for data model definition.","package":"pydantic","optional":false}],"imports":[{"symbol":"PydanticObjectType","correct":"from graphene_pydantic import PydanticObjectType"},{"symbol":"PydanticInputObjectType","correct":"from graphene_pydantic import PydanticInputObjectType"}],"quickstart":{"code":"import uuid\nimport pydantic\nimport graphene\nfrom graphene_pydantic import PydanticObjectType\n\nclass PersonModel(pydantic.BaseModel):\n    id: uuid.UUID\n    first_name: str\n    last_name: str\n\nclass Person(PydanticObjectType):\n    class Meta:\n        model = PersonModel\n        exclude_fields = (\"id\",)\n\nclass Query(graphene.ObjectType):\n    people = graphene.List(Person)\n\n    @staticmethod\n    def resolve_people(parent, info):\n        # In a real application, you would fetch data from a database\n        return [\n            PersonModel(id=uuid.uuid4(), first_name=\"Alice\", last_name=\"Smith\"),\n            PersonModel(id=uuid.uuid4(), first_name=\"Bob\", last_name=\"Johnson\")\n        ]\n\nschema = graphene.Schema(query=Query)\n\nquery = \"\"\"\nquery {\n  people {\n    firstName,\n    lastName\n  }\n}\n\"\"\"\n\nresult = schema.execute(query)\nprint(result.data['people'])\n# Expected output: [{'firstName': 'Alice', 'lastName': 'Smith'}, {'firstName': 'Bob', 'lastName': 'Johnson'}]","lang":"python","description":"This quickstart demonstrates how to define a Pydantic model and automatically convert it into a Graphene `ObjectType` using `PydanticObjectType`. It then sets up a basic GraphQL schema and executes a sample query."},"warnings":[{"fix":"Upgrade Python to 3.7+ and Pydantic to 1.7+ before upgrading graphene-pydantic to 0.3.0 or later.","message":"Version 0.3.0 dropped support for Python 3.6 and Pydantic versions older than 1.7. Ensure your environment meets these minimum requirements.","severity":"breaking","affected_versions":"0.3.0 and later"},{"fix":"Upgrade Pydantic to a 1.x version first, addressing any Pydantic-specific breaking changes, then upgrade graphene-pydantic.","message":"Version 0.1.0 removed support for Pydantic 0.x. If migrating from very old Pydantic versions, significant changes may be required.","severity":"breaking","affected_versions":"0.1.0 and later"},{"fix":"Represent dictionary fields as `JSONString` or define custom scalar types/Graphene `ObjectType`s for complex mapping structures, and provide custom resolvers.","message":"Due to a GraphQL limitation, Pydantic fields that hold mappings (e.g., dictionaries) cannot be directly exported to Graphene types.","severity":"gotcha","affected_versions":"All"},{"fix":"Avoid `Union` types in `PydanticInputObjectType` fields. Consider using separate input types for each union member or redesigning the input structure.","message":"GraphQL Input Object Types do not support unions as fields. Attempting to use a Pydantic `Union` type in an `PydanticInputObjectType` will lead to errors.","severity":"gotcha","affected_versions":"All"},{"fix":"Implement `is_type_of` in Graphene models representing union members. For `Union[Subclass, Baseclass]`, define as `Union[Subclass, Baseclass]`.","message":"When using `Union` types in `PydanticObjectType`, you must explicitly implement the `is_type_of` class method in your Graphene models. For unions between subclasses, the subclass must be listed first in the type annotation to ensure correct resolution.","severity":"gotcha","affected_versions":"All"},{"fix":"Refer to Pydantic's official migration guide for V1 to V2 changes. Use Pydantic's `bump-pydantic` tool for automated code transformation where possible. Thoroughly test your Graphene schema after Pydantic model updates.","message":"The library supports Pydantic versions `~1.7` through `~2.x`. However, Pydantic itself introduced significant breaking changes between V1 and V2. While `graphene-pydantic` aims to be compatible, migrating your underlying Pydantic models from V1 to V2 may still require substantial refactoring.","severity":"gotcha","affected_versions":"All (especially when migrating Pydantic versions)"}],"env_vars":null,"search_vec":"'0.6.1':12 'activ':36 'address':40 'api':52 'automat':17 'basemodel':20 'compat':45 'convert':18 'current':9 'develop':37 'generat':32,51 'graphen':1,8,23,43,46 'graphql':30,48 'inputobjecttyp':27 'integr':3,6 'librari':34 'objecttyp':24 'provid':14 'pydant':2,5,19,41,47 'schema':31,50 'schema-gener':49 'see':35 'streamlin':29 'updat':39 'util':15 'version':11,44","created_at":"2026-04-15T18:37:25.006181+00:00","updated_at":"2026-04-16T15:27:26.758034+00:00","problems":[{"fix":"Exclude the dictionary field using `exclude_fields` in the `PydanticObjectType.Meta` class, or convert the dictionary to a supported Graphene type manually via a custom `graphene.Field` and a `resolve_` method.","cause":"Graphene's type system does not directly support dictionary (mapping) types, so Pydantic models containing `dict` or `typing.Dict` fields cannot be automatically converted to Graphene `ObjectType`s by `graphene-pydantic`.","error":"Don't know how to handle mappings in Graphene."},{"fix":"Ensure that nested Pydantic models intended for input are also defined as `PydanticInputObjectType`s and that GraphQL type system constraints (e.g., no unions in input types) are respected. For circular references or complex nested inputs, ensure all types are properly registered and potentially use `resolve_placeholders()` if forward references are involved.","cause":"This error often occurs when attempting to use Pydantic models with complex structures (like nested models or models utilizing discriminators) as `PydanticInputObjectType`s for GraphQL mutations, as GraphQL input types have limitations on complexity and type resolution.","error":"TypeError: Input fields cannot be resolved. The input field type must be a GraphQL input type."},{"fix":"Use `default_factory` for mutable default values in your Pydantic `BaseModel`s. For example, instead of `field: list[str] = []`, use `field: list[str] = Field(default_factory=list)`.","cause":"This is a Pydantic validation error that occurs when a mutable object (like a list or dictionary) is used directly as a default value in a Pydantic `BaseModel` field, which can lead to unexpected shared state across instances. `graphene-pydantic` processes these models, exposing this underlying Pydantic issue.","error":"ValueError: mutable default <class 'list'> for field field is not allowed: use default_factory"},{"fix":"Ensure all type annotations are correctly imported and accessible. For forward references, make sure the referenced type is defined in the module, or provide a `_types_namespace` if the model is defined within a function or a local scope. If using circular references with `graphene-pydantic`, call `resolve_placeholders()` on your `PydanticObjectType`s after all models are defined.","cause":"This Pydantic error indicates that a type annotation in your Pydantic `BaseModel` could not be resolved, often due to a forward reference (a string literal for a type that is defined later) that hasn't been properly handled or imported, preventing `graphene-pydantic` from correctly introspecting the model.","error":"PydanticUndefinedAnnotation: name '...' is not defined"}],"ecosystem":"pypi","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":"0.6.1","cli_name":"","cli_version":null,"type":"library","homepage":null,"github":"https://github.com/graphql-python/graphene-pydantic","docs":null,"changelog":null,"pypi":"https://pypi.org/project/graphene-pydantic/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null,"categories":["web-framework","serialization","database"],"base_url":null,"auth_type":null,"provenance":{"verified_status":"passing","verified_at":"2026-06-28","last_verified":"2026-06-28","next_check":"2026-07-28","install_tag":null}}