hashie/hashie · error · KeyError

key not found with value of #{value.inspect}

Error message

key not found with value of #{value.inspect}

What it means

Error "key not found with value of #{value.inspect}" thrown in hashie/hashie.

Source

Thrown at lib/hashie/extensions/strict_key_access.rb:71

        raise DefaultError
      end

      def default=(_)
        raise DefaultError
      end

      def default_proc
        raise DefaultError
      end

      def default_proc=(_)
        raise DefaultError
      end

      def key(value)
        super.tap do |result|
          if result.nil? && (!key?(result) || self[result] != value)
            raise KeyError, "key not found with value of #{value.inspect}"
          end
        end
      end
    end
  end
end

View on GitHub (pinned to fde8e03a0a)

Solutions

  1. Ensure a key with exactly that value exists before calling hash.key(value); the comparison uses ==, so types must match.
  2. Use hash.rassoc or iterate hash.each to find near-matches if the value may differ slightly.
  3. Rescue KeyError or check hash.value?(value) first if a missing value is an expected case.

Example fix

hash.key(value) if hash.value?(value) # verify the value exists first, or rescue KeyError around the lookup.

When it happens

Trigger: Calling hash.key(value) on a Hash including Hashie::Extensions::StrictKeyAccess when no key maps to that value.

Common situations: Reverse-lookup of a key by value where the value is absent, on a strict hash that converts the nil result into a KeyError.


AI-assisted analysis of hashie/hashie@fde8e03a0a (2026-08-23). Data as JSON: /api/errors/6283085ebbbf456b. Report an issue: GitHub.