Apparently I was using a value when I should have used a pointer and my IDE did not detect the problem either:
func (p *Service) NewError(ctx context.Context, err error) (r *api.ErrorStatusCode) {
var securityError *ogenerrors.SecurityError
if ok := errors.As(err, &securityError); ok {
// This works now
log.Println("this is a security error", err)
}
}