iflytek/astron-agent · error · ThirdPartyException

MD5 computation error

Error message

MD5 computation error: {e}

What it means

md5() in core/knowledge/utils/spark_signature.py wraps hashlib.md5 hexdigest computation for Spark/讯飞 API signatures. Any failure encoding the cipher text or hashing raises ThirdPartyException so callers of get_signature see a normalized third-party error. In practice md5 over utf-8 bytes essentially never fails; this guards unexpected runtime conditions.

Solutions

  1. Check the origin of cipher_text; decode bytes with errors='replace' before passing it in
  2. Ensure text is a str, not bytes or None, before calling get_signature
  3. Log the inner exception (already logged via logger.error) to find the true cause

Example fix

// before
sig = md5(raw_bytes)  # raises ThirdPartyException
// after
sig = md5(raw_bytes.decode('utf-8', errors='replace'))
Defensive patterns

Strategy: try-catch

Validate before calling

if not isinstance(cipher_text, str): raise TypeError('cipher_text must be str')
cipher_text.encode('utf-8')  # pre-validate encodability

Type guard

def is_valid_text(v) -> bool: return isinstance(v, str)

Try / catch

try:
    sig = get_signature(...)
except ThirdPartyException as e:
    logger.warning('signature failed: %s', e); sig = None

Prevention

When it happens

Trigger: Calling get_signature() when cipher_text contains characters that break encode('utf-8') (surrogates from bad decoding) or any unexpected exception inside the hashing block.

Common situations: Upstream text decoded with errors='surrogateescape' from a malformed response, or monkeypatched/unavailable hashlib in restricted environments.

Understand the failure class

Background: Checksum mismatch errors: "checksum verification failed", "digest mismatch", "expected vs actual checksum" — what they mean and how to fix them — this error's family across 41 libraries.

Related errors


AI-assisted analysis of iflytek/astron-agent@5e758547a8 (2026-09-12). Data as JSON: /api/errors/b9f44db23261811d. Report an issue: GitHub.

Appendix: source

Thrown at core/knowledge/utils/spark_signature.py:47

def md5(cipher_text: str) -> str:
    """
    Generate MD5 hash value.

    Args:
        cipher_text: Text to be hashed

    Returns:
        MD5 hash string
    """
    try:
        data = cipher_text.encode("utf-8")
        md = hashlib.md5()
        md.update(data)
        return md.hexdigest()
    except Exception as e:
        logger.error(f"MD5 computation failed: {e}")
        raise ThirdPartyException(f"MD5 computation error: {e}")


def hmac_sha1_encrypt(encrypt_text: str, encrypt_key: str) -> str:
    """
    Encrypt text using HMAC-SHA1.

    Args:
        encrypt_text: Text to be encrypted
        encrypt_key: Encryption key

    Returns:
        Base64 encoded encryption result
    """
    try:
        secret_key = encrypt_key.encode("utf-8")
        text = encrypt_text.encode("utf-8")
        mac = hmac.new(secret_key, text, hashlib.sha1)
        return base64.b64encode(mac.digest()).decode("utf-8")

View on GitHub (pinned to 5e758547a8)