GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Raise ValidationError for invalid values in Django vector fields by mayuriphad · Pull Request #172 · pgvector/pgvector-python

Conversation

@mayuriphad

@mayuriphad mayuriphad commented Oct 25, 2026

Copy link
Copy Markdown

Fixes #171

VectorField, HalfVectorField and SparseVectorField pass form and serializer input straight to the text parsers. A malformed value made form.is_valid() raise ValueError, and a value with the wrong number of dimensions passed validation and only failed on save() with a DataError from Postgres. In both cases the request ended in a 500.

Changes:

  • to_python turns parse errors into ValidationError (Enter a valid vector. and the half/sparse variants).
  • Each field adds a validator, the way max_length adds MaxLengthValidator. It checks that the value converts to the vector type and, when dimensions is set, that the length matches (Expected 3 dimensions, not 2.). Since it is in validators, DRF's ModelField also runs it, and ArrayField(VectorField(dimensions=3)) reports the bad item.
  • Valid input behaves as before. Fields without dimensions skip the length check.

Tests: new cases in tests/test_django.py for full_clean and the three model forms, covering malformed input and wrong dimensions. The full suite, mypy and ty check pass on my fork's CI run (Postgres with pgvector 0.8.7).

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
mayuriphad added 2 commits October 25, 2026 10:52
@mayuriphad
Raise ValidationError for invalid vectors in Django fields
f5b4b8b
VectorField, HalfVectorField and SparseVectorField passed malformed
input straight to the parsers, so a ModelForm or serializer raised
ValueError instead of reporting a field error. A value with the wrong
number of dimensions passed validation and failed later with DataError
from Postgres.

Convert parse errors to ValidationError in to_python and add a
validator that checks the dimensions when the field has them.

Fixes pgvector#171

Signed-off-by: mayuriphad <[email protected]>
@mayuriphad
Check the parsed vector for None instead of ignoring the type
271e966
Signed-off-by: mayuriphad <[email protected]>
Copilot AI balanced review requested due to automatic review settings October 25, 2026 05:32
Copilot AI reviewed Oct 25, 2026

Copilot AI left a comment

Copy link
Copy Markdown

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

Copilot code review Copilot
Copilot review effort
Applies to this pull request for everyone.Learn more about Copilot code review.
Copilot left review comments

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Django: invalid VectorField input raises ValueError / DataError instead of a validation error

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@mayuriphad
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.