Skip to content

Python: fix(embeddings): wrap undecodable base64 payloads in a framework error - #9705

Merged
Eduard van Valkenburg (eavanvalkenburg) merged 1 commit into
microsoft:mainfrom
lisry-1:fix/openai-embedding-base64-error
Oct 29, 2026
Merged

Eduard van Valkenburg (eavanvalkenburg) merged 1 commit into
microsoft:mainfrom
lisry-1:fix/openai-embedding-base64-error

Conversation

@lisry-1

@lisry-1 lisry-1 commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

Motivation & Context

With encoding_format="base64", both embedding clients call base64.b64decode on each payload before unpacking it as little-endian float32 values. A payload that cannot be decoded at all escapes as a bare binascii.Error (bad padding/alphabet) or UnicodeEncodeError (non-ASCII input that str.encode("ascii") rejects), with no reference to the input that broke — so a malformed response is indistinguishable from a bug in the client.

Reported in #9511.

Relationship to #9512

#9512 (merged) already reports a misaligned payload — one whose decoded length is not a multiple of 4 — for the OpenAI client. This PR deliberately scopes itself to the remaining, still-unhandled path: a payload that cannot be decoded at all. It adds that path to the OpenAI client, and adds both guards to the Foundry client (which #9512 did not touch).

Description & Review Guide

  • What are the major changes?

    python/packages/openai/agent_framework_openai/_embedding_client.py and python/packages/foundry/agent_framework_foundry/_embedding_client.py:

    Well-formed payloads are unaffected; the guard only rejects input that would have failed during b64decode or struct.unpack anyway.

  • What is the impact of these changes?

    Callers that previously caught binascii.Error / UnicodeEncodeError now see IntegrationInvalidResponseException, the public agent_framework.exceptions type used for invalid integration responses.

  • What do you want reviewers to focus on?

    The exception type. IntegrationInvalidResponseException is the closest match in the existing hierarchy for "the service returned something we cannot interpret".

Related Issue

Fixes #9511

Contribution Checklist

AI Assistance

  • An AI coding assistant helped locate the defect, draft the fix, and write the tests.
  • I understand and reviewed every change in this pull request.
  • The reproduction and all tests were run locally.

Test runs (after rebase onto main): test_openai_embedding_client.py 22 passed / 3 skipped; test_foundry_embedding_client.py 32 passed / 2 skipped.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Non-ASCII payloads still escape both handlers as unannotated ValueError exceptions, and Foundry’s decode-error handler lacks regression coverage.

2 open issues
What changed in this PR

This PR addresses #9511 by giving malformed base64 embedding responses contextual framework errors in the Python OpenAI and Foundry integrations.

Changes:

  • Wraps base64 decoding errors in IntegrationInvalidResponseException.
  • Rejects decoded lengths that are not divisible by four, reporting the input index and byte count.
  • Adds regression tests for malformed payloads.
File Description
python/​packages/​openai/​tests/​openai/​test_openai_embedding_client.py Tests invalid decoded lengths and undecodable payloads.
python/​packages/​openai/​agent_framework_openai/​_embedding_client.py Adds contextual errors around base64 decoding and length validation.
python/​packages/​foundry/​tests/​foundry/​test_foundry_embedding_client.py Tests invalid decoded lengths in project mode.
python/​packages/​foundry/​agent_framework_foundry/​_embedding_client.py Adds equivalent validation using original input indices.

Review effort: Balanced

Comment thread python/packages/openai/agent_framework_openai/_embedding_client.py Outdated
@lisry-1
lisry-1 temporarily deployed to github-app-auth October 26, 2026 13:17 — with GitHub Actions Inactive
…ork error

With `encoding_format="base64"`, both embedding clients call
`base64.b64decode` on each payload before unpacking it as little-endian
float32 values. A payload that cannot be decoded at all (bad padding or
alphabet, or non-ASCII input that `str.encode('ascii')` rejects) escapes as
a bare `binascii.Error` / `UnicodeEncodeError`, with no reference to the
input that broke, so a malformed response is indistinguishable from a bug
in the client.

Both `binascii.Error` and `UnicodeEncodeError` are `ValueError` subclasses,
so the decode is wrapped in `except ValueError` and re-raised as
`IntegrationInvalidResponseException`, naming the input index and preserving
the original cause.

`microsoft#9512` already reports a *misaligned* payload (decoded length not a multiple
of 4) for the OpenAI client, so this change intentionally only adds the
missing undecodable-payload path there; the Foundry client had neither guard,
so it gains both.

Regression tests cover an undecodable payload and a non-ASCII payload for
OpenAI, and a 5-byte payload and an undecodable payload for Foundry.
@lisry-1
lisry-1 force-pushed the fix/openai-embedding-base64-error branch from 32e6c7e to c805f65 Compare October 28, 2026 08:51
@lisry-1
lisry-1 temporarily deployed to github-app-auth October 28, 2026 08:51 — with GitHub Actions Inactive
@lisry-1 lisry-1 changed the title Python: fix(embeddings): report malformed base64 payloads instead of raising struct.error Python: fix(embeddings): wrap undecodable base64 payloads in a framework error Oct 28, 2026
@lisry-1
lisry-1 temporarily deployed to github-app-auth October 28, 2026 08:52 — with GitHub Actions Inactive
@eavanvalkenburg

Copy link
Copy Markdown
Member

lisry-1 Thanks for clarifying the scope relative to #9512. This is an agreed follow-up to #9511: the remaining undecodable-payload handling and Foundry parity are in scope. IntegrationInvalidResponseException is acceptable for these new guards; the existing OpenAI misaligned-length ValueError can stay unchanged in this PR.

Merged via the queue into microsoft:main with commit 30ce7e3 Oct 29, 2026
54 checks passed

This branch was previously deployed

1 inactive deployment
github-app-auth — c805f65f Deployed Oct 28, 2026 by lisry-1 via add_label #26391
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python Usage: [Issues, PRs], Target: Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python: malformed base64 embedding payload raises a bare struct error with no context

3 participants