Repository navigation
fix(pr_agent/tools/pr_similar_issue.py): migrating embeddings to the openai v1 client - #2726
Conversation
…1 embeddings client and the all-zero vector guard
PR Summary by QodoMigrate similar-issue embeddings to OpenAI v1 client and guard against all-zero indexing
AI Description
Diagram
High-Level Assessment
Files changed (2)
|
Code Review by Qodo
1.
|
|
Code review by qodo was updated up to the latest commit da48d5f |
| except Exception: | ||
| failures += 1 | ||
| embeds.append([0] * 1536) | ||
| if failures == len(list_to_encode): |
There was a problem hiding this comment.
This block is identical at 489, 588 and 690, and nothing in the suite executes it. Lifting it up beside _embed fixes both at once:
def _embed_with_fallback(texts: List[str]) -> List[List[float]]:
"""Embed a list, falling back to one by one, and refuse to return an all-zero set."""
try:
return _embed(texts)
except Exception as e:
get_logger().error("Failed to embed entire list, embedding one by one...",
artifact={"error": str(e)})
embeds = []
failures = 0
for text in texts:
try:
embeds.append(_embed([text])[0])
except Exception:
failures += 1
embeds.append([0] * 1536)
if failures == len(texts):
raise RuntimeError(
"Failed to embed any issue text; refusing to index all-zero vectors") from e
return embedsEach of the three sites then reads embeds = _embed_with_fallback(list_to_encode). I applied exactly this and the merged suite stayed at 2042 passed.
| assert calls["model"] == psi.MODEL | ||
|
|
||
|
|
||
| def test_a_total_embedding_failure_raises_instead_of_indexing_zero_vectors(monkeypatch): |
There was a problem hiding this comment.
This asserts that _embed propagates a client-construction error, not the indexing guard the name describes. That guard never executes in the suite. With the helper above it becomes two short tests, both of which I ran green: a total failure raises, and a partial failure zeroes only the failing item.
IsmaelMartinez
left a comment
There was a problem hiding this comment.
Thanks for the diagnosis here, and sorry it sat.
Rather than leave a live breakage waiting on a round trip, I pushed the lift itself as ff572e34. _embed_with_fallback now sits beside _embed, so the three byte-identical blocks at 477, 576 and 678 each read one line. Your all-zero guard is unchanged, just in one place instead of three. The test at line 58 became two, because it was asserting that _embed propagates a client-construction error rather than the indexing guard its name described: one case for a total failure raising, one for a partial failure zeroing only the item that failed.
Your commit is still an ancestor and it went up as a fast-forward, so nothing of yours was rewritten. 2763 passed against 2757 on main, ruff clean, and all six error out on revert.
Merging, which closes #2725. The breakage was real: main still calls openai.Embedding.create at line 279 against an openai>=1.55.3 pin.
Closes #2725.
Description
requirements.txtpinsopenai>=1.55.3(installed: 2.54.0), butpr_similar_issue.pystill callsopenai.Embedding.createand setsopenai.api_key— the v0 interface removed in openai 1.0.Root cause
Nine call sites (lines 279, 283, 460, 463, 470, 556, 559, 655, 658, 665) use the openai v0
interface.
openai.Embeddingand module-levelopenai.api_keywere removed in openai 1.0; thereplacement is
OpenAI(api_key=...).embeddings.create(input=..., model=...)with attribute accesson the response (
response.data[i].embedding) instead of subscripting.The bare
except:handlers around the indexing paths turn the hard failure into silent datacorruption, which is why the breakage has gone unnoticed.
The fix
_embed()helper builds anopenai.OpenAIclient and callsclient.embeddings.create,returning
[record.embedding for record in response.data].except:handlers becomeexcept Exception, log the underlying error, and — whenevery item fails — raise instead of indexing an all-zero vector set. A partial failure still
degrades to
[0] * 1536for the affected items only, as before.Behaviour change
/similar_issueraisesAPIRemovedInV1; indexing silently stores all-zero vectors/similar_issueuses the pinned openai client; a total embedding failure raises instead of corrupting the indexFiles changed
Testing
New regression coverage in
tests/unittest/test_similar_issue_embeddings.py— 4 tests, each written to failwithout the fix:
Proven to be a genuine regression test: with every changed
pr_agent/file reverted to itsorigin/mainversion and the new test file left in place, the suite fails. It only passeswith the fix applied.
Full pipeline, reproduced locally exactly as
.github/workflows/build-and-test.yamlruns it:on
python:3.12.13-slim— no failures.Also checked:
pytest tests/unittest— no new failures vsmainruff— no new findings vsmain;isort— clean on every file touchedRisk / compatibility
The embedding request itself is unchanged (same model, same inputs). The only behaviour change
beyond the API migration is that a total embedding failure now raises rather than indexing
useless vectors — a failure that previously produced a silently broken index.
pinecone,pinecone-datasetsandlancedbremain commented out ofrequirements.txt, so thebackends still have to be installed separately; that is out of scope for this fix.
Checklist