Skip to content

Schema mismatch not detected locally when serializing Number/String attributes #1285

@Abhi904485

Description

@Abhi904485

When using PynamoDB with DynamoDB Local (amazon/dynamodb-local:latest), schema validation errors are not surfaced.
This leads to code that works against local DynamoDB but fails in production AWS DynamoDB with SerializationException.

Actual behavior

In production AWS DynamoDB:
{
  "Error": {
    "Code": "SerializationException",
    "Message": "NUMBER_VALUE cannot be converted to String"
  }
}

But when running locally with:

dynamodb-local:
  image: "amazon/dynamodb-local:latest"
  command: "-jar DynamoDBLocal.jar -sharedDb -dbPath ./data"
  ports:
    - "8030:8000"

the same write succeeds silently, even when attribute types are mismatched.

Example Code:

from pynamodb.models import Model
from pynamodb.attributes import UnicodeAttribute, NumberAttribute

class MyModel(Model):
    class Meta:
        table_name = "TestTable"
        host = "http://localhost:8030"

    id = UnicodeAttribute(hash_key=True)
    count = NumberAttribute()

# Works locally but fails on real DynamoDB
item = MyModel(id="123", count="not_a_number")
item.save()

Local DynamoDB → succeeds, record written with wrong type
AWS DynamoDB → raises SerializationException: NUMBER_VALUE cannot be converted to String

Proposal

Could PynamoDB add stricter client-side validation to catch these mismatches before sending the request?

Options:

  • Enforce type checking in Attribute.serialize (e.g., NumberAttribute should only accept int/float, not str).
  • Raise a ValueError or PynamoDB-specific ValidationError when serialization is impossible.
  • Provide a setting like STRICT_VALIDATION=True in Meta to toggle this behavior, so existing code isn’t broken.

This would make local DynamoDB testing more consistent with real AWS behavior.

Environment

  • Python: 3.10 (prod), 3.11 (local)
  • PynamoDB version: 6.0.2
  • boto3: 1.28.26
  • botocore: ==1.31.26
  • DynamoDB Local: amazon/dynamodb-local:latest
  • AWS DynamoDB: ap-south-1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions