Repository navigation
fix(openai): count base64 embedding vector dimensions - #2077
Open
as992949791 wants to merge 2 commits into
Open
as992949791 wants to merge 2 commits into
as992949791 wants to merge 2 commits into
Conversation
Generated with OpenAI Codex.
Generated with OpenAI Codex.
|
|
| if isinstance(embedding_vector, str): | ||
| try: | ||
| # Base64 embeddings contain one float32 per dimension. | ||
| dimensions = len(base64.b64decode(embedding_vector)) // 4 |
Contributor
There was a problem hiding this comment.
Malformed strings bypass the fallback
base64.b64decode silently discards invalid characters by default. A provider response of "!!!!" therefore exports 0 dimensions instead of the previous 4. Likewise, "AAAA" decodes to three bytes, which // 4 truncates to 0 despite not containing a complete float32.
Use strict decoding and check that the decoded length is divisible by four before counting dimensions. Add both cases to the fallback tests.
Prompt To Fix With AI
This is a comment left during a code review.
Path: langfuse/openai.py
Line: 971
Comment:
**Malformed strings bypass the fallback**
`base64.b64decode` silently discards invalid characters by default. A provider response of `"!!!!"` therefore exports 0 dimensions instead of the previous 4. Likewise, `"AAAA"` decodes to three bytes, which `// 4` truncates to 0 despite not containing a complete float32.
Use strict decoding and check that the decoded length is divisible by four before counting dimensions. Add both cases to the fallback tests.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
An explicit
encoding_format="base64"leaves the embedding as a string in the OpenAI SDK. Langfuse currently records that string's character count as the vector dimensions: a three-dimensional float32 embedding is reported as 16 dimensions.Count the decoded float32 elements for base64 responses. Float-array responses keep their current behavior, and the SDK response is returned unchanged. The exported output still contains only dimensions and count. If a provider returns malformed base64, decoding falls back to the previous string-length result without changing the SDK passthrough. Only decoding errors are caught; SDK call errors retain their existing behavior.
Type of change
Verification
uv run --frozen pytest tests/unit/test_openai.py tests/unit/test_openai_prompt_extraction.py tests/unit/test_parse_usage_model.py uv run --frozen ruff check . uv run --frozen ruff format --check langfuse/openai.py tests/unit/test_openai.py uv run --frozen mypy langfuse --no-error-summary75 tests passed; Ruff and mypy passed. All five new cases use the real OpenAI response parser with
httpx.MockTransportand the shared in-memory exporter. No live provider or Langfuse server calls were made.Evidence
Before: the three valid-base64 export regressions fail on upstream, reporting string lengths instead of 1, 2, or 3 dimensions. After: all five new cases pass, including malformed-base64/non-ASCII passthrough and equality of the SDK-returned embedding string. The existing float-array export regression also passes.
Merge Danger
Door: two-way. Blast Radius: embedding trace output for explicit base64 responses. No public signatures or provider responses change.
Checklist
code_review.md(Codex review)..env.templateif needed. No documentation becomes stale from correcting this count.AI disclosure: The implementation, regression tests, and self-review were produced with OpenAI Codex. No human review is claimed.
The PR appears safe to merge, with a non-blocking gap in the malformed-response fallback.
Summary
Corrects embedding dimension counts by decoding base64 strings and counting float32 elements.
Reviews (1) · Last reviewed commit: "fix(openai): preserve malformed embeddin..." · Reviewed by Greptile