You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I added a very descriptive title to this question.
I searched the LangChain documentation with the integrated search.
I used the GitHub search to find a similar question and didn't find it.
Commit to Help
I commit to help with one of those options 👆
Example Code
importosimportjsonimporttimeimportreimportbase64fromioimportBytesIOfromlangchain_community.document_loaders.parsers.pdfimportPyMuPDFParserfromlangchain_community.document_loaders.blob_loaders.schemaimportBlobdefparse_multipart_formdata(body, content_type):
"""Parses multipart/form-data from raw event body."""# Extract boundary from content typeboundary=content_type.split("boundary=")[-1]
boundary_bytes=f"--{boundary}".encode()
# Split the body using the boundaryparts=body.split(boundary_bytes)
file_content=Nonefilename=Noneforpartinparts:
ifb"Content-Disposition"inpartandb"filename="inpart:
# Extract file namedisposition_match=re.search(rb'filename="(.+?)"', part)
ifdisposition_match:
filename=disposition_match.group(1).decode()
# Extract file content (after double CRLF)file_content=part.split(b"\r\n\r\n", 1)[-1].rstrip(b"--\r\n")
break# Stop after finding the fileifnotfile_content:
raiseValueError("No file found in request body")
returnfilename, BytesIO(file_content)
deflambda_handler(event, context):
print("Upload call initiated")
headers= {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET, POST, DELETE, OPTIONS",
"Access-Control-Allow-Headers": "Content-Type,X-Amz-Date,Authorization,X-Api-Key",
}
start_time=time.time()
try:
if"body"notinevent:
raiseValueError("Missing request body")
if"headers"notineventor"content-type"notinevent["headers"]:
raiseValueError("Missing Content-Type header")
# Extract content typecontent_type=event["headers"]["content-type"]
# Parse file from the raw multipart bodyfilename, file_stream=parse_multipart_formdata(event["body"].encode(), content_type)
print(f"Received file: {filename}, Size: {len(file_stream.getvalue())} bytes")
blob=Blob(data=file_stream.getvalue())
print(f"Type of blob: {type(blob)}")
# Process with PyMuPDFParserparser=PyMuPDFParser()
pages=parser.parse(blob)
text="\n".join([page.page_contentforpageinpages])
return {
'statusCode': 200,
"body": json.dumps({"message": text}),
"headers": headers,
}
exceptExceptionase:
print(f"Error processing request: {e}")
return {
'statusCode': 400,
'body': json.dumps({'error': f'Error processing file: {str(e)}'}),
"headers": headers,
}
finally:
print(f"Total execution time: {time.time() -start_time:.2f} seconds")
Description
Hello everyone,
I’m encountering an unusual issue while trying to extract text from a PDF in AWS Lambda. I have a function that works perfectly when run locally with a Flask API, but when uploaded to AWS Lambda, it returns an empty string ("") instead of the expected text from the PDF.
The function executes without errors, and the Lambda logs don't indicate any problems.
Has anyone encountered this issue before or have suggestions on what might be causing the discrepancy between local execution and Lambda?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
Checked other resources
Commit to Help
Example Code
Description
Hello everyone,
I’m encountering an unusual issue while trying to extract text from a PDF in AWS Lambda. I have a function that works perfectly when run locally with a Flask API, but when uploaded to AWS Lambda, it returns an empty string ("") instead of the expected text from the PDF.
The function executes without errors, and the Lambda logs don't indicate any problems.
Has anyone encountered this issue before or have suggestions on what might be causing the discrepancy between local execution and Lambda?
System Info
langchain==0.3.14
langchain-community==0.3.14
langchain-core==0.3.31
langchain-groq==0.2.3
langchain-qdrant==0.2.0
langchain-text-splitters==0.3.5
Beta Was this translation helpful? Give feedback.
All reactions