SeleniumHQ/selenium · error · Error::SerializationError

#{name}##{field.name} expected a [key, value] pair, got #{el

Error message

#{name}##{field.name} expected a [key, value] pair, got #{element.inspect}

What it means

For a BiDi map field (a scalar-typed union serialized as [key, value] pairs), read_map_entry expects each element to be a 2-item Array. An element that is not a 2-element array is malformed and raises Error::SerializationError. This guards the entry-level shape of BiDi map structures.

Source

Thrown at rb/lib/selenium/webdriver/bidi/serialization/record.rb:317

              raw.map do |element|
                if field.scalar
                  read_map_entry(field, element, klass)
                elsif element.is_a?(::Array)
                  read_list(field, element, klass)
                else
                  klass.from_json(element)
                end
              end
            end

            # A map entry is a `[key, value]` pair. The key is `Ref / text` — an object key
            # deserializes, a bare-string key passes through once validated against the arm's
            # primitive. The value is the object-only Ref and always deserializes, so a bare scalar
            # there is rejected (object_only holds at the value position). A non-pair element is a
            # malformed entry and is rejected outright.
            def read_map_entry(field, element, klass)
              unless element.is_a?(::Array) && element.size == 2
                raise Error::SerializationError,
                      "#{name}##{field.name} expected a [key, value] pair, got #{element.inspect}"
              end

              key, value = element
              key = key.is_a?(::Hash) ? klass.from_json(key) : scalar_value(field, key)
              [key, klass.from_json(value)]
            end

            # A bare scalar at a scalar-tolerant union position must match one of the union's
            # scalar-arm primitives (+scalar+ is a primitive name or an array of them); a
            # wrong-typed scalar (a number where a string is expected) is a wire error, not
            # something to pass through. An unrecognized primitive (none in PRIMITIVE_TYPES) is
            # left unchecked, matching the lenient default elsewhere.
            def scalar_value(field, value)
              expected = Array(field.scalar).flat_map { |primitive| PRIMITIVE_TYPES[primitive] || [] }
              return value if expected.empty? || expected.any? { |type| value.is_a?(type) }

              raise Error::SerializationError,

View on GitHub (pinned to aa36b38e69)

Solutions

  1. Capture the raw list to inspect the malformed element.
  2. Confirm the map encoding ([key, value] pairs) is what the browser should emit per spec.
  3. Align Selenium and browser versions.
  4. Report the malformed entry upstream.
Defensive patterns

Strategy: validation

Validate before calling

def pair?(element)
  element.is_a?(::Array) && element.size == 2
end

Type guard

def bidi_map_pair?(element)
  element.is_a?(::Array) && element.size == 2
end

Try / catch

begin
  record = SomeRecord.from_json(payload)
rescue Selenium::WebDriver::Error::SerializationError => e
  raise unless e.message =~ /expected a .key, value. pair/
  warn "malformed map entry: #{e.message}"
end

Prevention

When it happens

Trigger: Inbound BiDi map payload where an element is not a [key, value] pair — e.g. a single scalar, an object, or an array of length != 2 at a position deserialized via read_map_entry.

Common situations: Browser or grid emits a malformed map encoding, schema skew on a scalar/map union field, non-conformant BiDi implementation.

Related errors


AI-assisted analysis of SeleniumHQ/selenium@aa36b38e69 (2026-08-14). Data as JSON: /api/errors/631184616e46e3db. Report an issue: GitHub.