Fixed error 1 (openAI title sanitization) and added unit tests#466
Fixed error 1 (openAI title sanitization) and added unit tests#466amahuli03 wants to merge 5 commits intoCodeForPhilly:developfrom
Conversation
@amahuli03 I merged your PR that fixed this issue (PR #468)! Do you anything from me for further work on this PR? |
|
@sahilds1 Nope, the other part of this issue is resolved now. I'll mark this as ready for review |
…sed, not where it's defined
| doc = MagicMock() | ||
| doc.metadata = {"title": None} | ||
| doc.get_text.return_value = [] | ||
| doc[0].get_text.return_value = [] |
There was a problem hiding this comment.
I also found an issue with how I'm setting up the mocks in all three tests. generate_title effectively does this to the pdf: pdf[0].get_text("blocks")
The tests were passing regardless because MagicMock will auto-create a new attribute on access, so, for the purposes of this test, it worked. I've fixed this actually make sense
Description
Fixes the
/v1/api/uploadFileendpoint failure that occurs when title extraction falls back to OpenAI. OpenAI sometimes returns titles wrapped in quotes (ex: '"Updated CANMAT/ISBD Guidelines..."'). The raw response is saved without sanitization, which caused a 400 error in the reported logs. This PR strips wrapping quotes and whitespace from OpenAI-generated titles before returning.This PR also truncates titles to 255 characters so the title is guaranteed to fit in the
CharFieldsize limit. This is just an extra safeguard in the case where OpenAI doesn't respect the 256 character limit specified in the prompt.Related Issue
Addresses part of #390
Manual Tests
(I'm not able to manually test right now, looking for help getting my local dev environment and workflow set up)
Automated Tests
Added 2 new unit tests to
test_title.py:test_strips_quotes_from_openai_titleverifies wrapping quotes are removed from OpenAI-generated titlestest_truncates_long_openai_titleverifies titles exceeding 255 characters are truncatedAll tests passing (4 existing + 2 new)
Reviewers
@sahilds1
Notes
Also added some more logs to the error handler in
views.pyso errors include full traceback.This only addresses the first error from the original issue. The second error is being caused by a separate bug which I think has to do with the upload component in
UploadFile.tsxwhere we're settingContent-Type: multipart/form-databut not providing a boundary parameter. Needs further investigation and manual testing.(This is a separate issue) When I tried uploading files manually in my local dev environment, I got
Unauthorized: /api/v1/api/uploadFilewhich is not a valid route, right? Has anyone seen this issue?