Skip to content

Add support for nullable vector columns#7

Closed
JulianPasquale wants to merge 1 commit intopgvector:masterfrom
JulianPasquale:fix/handle-nil-values
Closed

Add support for nullable vector columns#7
JulianPasquale wants to merge 1 commit intopgvector:masterfrom
JulianPasquale:fix/handle-nil-values

Conversation

@JulianPasquale
Copy link

Hi @ankane,

Looks like the gem doesn't handle nil values gracefully. I created this small script to demostrate the issues I found:

# frozen_string_literal: true

require "bundler/setup"
require "sequel"
require "pgvector"

DB = Sequel.connect("postgres://localhost/pgvector_ruby_test")
DB.run("CREATE EXTENSION IF NOT EXISTS vector")

DB.drop_table? :items
DB.create_table :items do
  primary_key :id
  column :embedding, "vector(3)"
end

# Sequel models
class Item < Sequel::Model(DB[:items])
  plugin :pgvector, :embedding
end

item_1 = Item.create(embedding: [1.0, 2.0, 3.0])
item_2 = Item.create

puts "✅ Item 1 embedding: #{item_1.embedding.inspect}"
puts "✅ Item 2 embedding: #{item_2.embedding.inspect}"

begin
  Item.create(embedding: nil) # ERROR: vector must have at least 1 dimension (PG::DataException)
rescue Sequel::DatabaseError => e
  puts "❌ Error inserting item with nil embedding: #{e.message}"
end

# Pgvector directly
begin
  Pgvector.decode(nil)  # => NoMethodError if embedding is NULL
rescue NoMethodError => e
    puts "❌ Error decoding nil embedding: #{e.message}"
end

I made some changes to support nil values but I'm not sure if this is the correct approach, maybe defining an empty string as default value for vector columns is just enough.

Feel free to close this Pull Request if the solution is not what you'd expect.

@ankane ankane closed this in 78627a1 Mar 17, 2026
@ankane
Copy link
Member

ankane commented Mar 17, 2026

Hi @JulianPasquale, thanks for reporting and the PR! Merged a version of this in the commit above.

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

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants