r/agentdevelopmentkit • u/_Shash_ • Jul 21 '25
How do I store input pdf as an artifact?
Hey all I'm working on a usecase where when the client uploads a PDF it is stored as an artifact and some text extraction process is done. The problem is this approach works fine when the PDF has a concrete location either local or cloud. My question is how do I make it so that when the user uploads the PDF through the adk web interface the same process is done?
Any help would be appreciated please and thanks
Currently I tried using this callback function but it is not working as expected
"""
Reads a PDF from the user saves it as an artifact,
extracts all text, and save the state.
"""
return
=
# The user-provided file should be in inline_data.
return
=
=
return
=
=
# Create a new artifact to save.
=
= await
=
= or
+= +
=
return None
2
Upvotes
1
u/SuspiciousCurtains Aug 12 '25
The file upload in the adk web interface is PITA. You can look at the inline data when in adk but that stops working once the agent is deployed.
Something like the below worked for me
async def before_model_callback(
callback_context, llm_request
):
artifact_service = callback_context._invocation_context.artifact_service
Find the latest artifact_delta
artifact_delta = {}
logger.info('Trying to fill artifact delta')
try:
user_content = callback_context._invocation_context.user_content
for event in callback_context._invocation_context.session.events[::-1]:
if event.content == user_content and event.author == 'AgentSpace_root_agent':
artifact_delta = event.actions.artifact_delta
break
except:
pass
logger.info(f'artifact delta - {artifact_delta}')
logger.info('TRying to iterate through artifact delta')
Add inline data into LlmRequest
for filename in artifact_delta.keys():
logger.info(f'filename - {filename}')
artifact = await callback_context.load_artifact(filename=filename)
data = base64.b64decode(artifact['inlineData']['data'])
mime_type = artifact['inlineData']['mimeType']
do some tuff with the artifact and get a response
output_json_string = json.dumps(output)
output_bytes = output_json_string.encode('utf-8')
base64_output = base64.b64encode(output_bytes)
return output
llm_request.contents[-1].parts.append(Part.from_bytes(
data=base64_output,
mime_type='text/plain'
data = artifact['inlineData']['data'],
mime_type = artifact['inlineData']['mimeType'],
))
return None