Rather than the assert, why isn't this function simply defined as strict?  To that end, wouldn't returning NULL in this case be more appropriate than raising an error?