Skip to content

GraphQLFloat should accept Python Decimal for database ORM compatibility #280

Description

@gabor-lbl

graphql-core's GraphQLFloat scalar rejects Python Decimal objects, which are the standard return type from database ORMs (SQLAlchemy, Django ORM, etc.) for:

  • Numeric/Decimal column types
  • Aggregate functions (SUM, AVG) even on Integer/Float columns
  • Any high-precision arithmetic

Current Behavior:

from decimal import Decimal
from graphql import GraphQLFloat

GraphQLFloat.serialize(Decimal("123.45"))
# Raises: GraphQLError: Float cannot represent non numeric value: <Decimal instance>

Expected Behavior:
Decimal should be coerced to float, just as int is already accepted.

Why This Matters:

  1. Python's Decimal is a numeric type - it's part of the standard library for precise decimal arithmetic
  2. Database ORMs universally return Decimal for precision:
    • SQLAlchemy: Numeric, DECIMAL columns → Decimal
    • Django ORM: DecimalField → Decimal
    • PostgreSQL aggregates often return NUMERIC → Decimal
  3. Precedent exists: GraphQLInt already accepts bool (coerces True/False → 1/0)
  4. Backwards compatibility: Earlier versions were more lenient

Workarounds:

  • Manually float() every Decimal in resolvers (verbose, error-prone)
  • Monkey-patch GraphQLFloat (fragile)
  • Custom scalar type (requires client-side changes)

Impact:
Every Python GraphQL server using database ORMs must implement workarounds. This makes graphql-core less pythonic and forces type coercion at the application layer.

Environment:

  • graphql-core: 3.x
  • Python: 3.12

Activity

  1. Cito commented on Oct 9, 2026

    @Cito
    Member

    Thanks a lot for reporting this, @gabor-lbl. It was actually a regression, it worked before in 3.2.

    Fixed in #281 with regression tests. The fix will be published in 3.3.1.

  2. self-assigned this
    on Oct 9, 2026
  3. added theissue type on Oct 9, 2026
  4. added
    bugSomething isn't working
    on Oct 9, 2026
  5. removed theissue type on Oct 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions