Async file search attachment API with Azure Agents returning: "Message: purpose contains an invalid purpose"

Patrick Kelly-Goss 20 Reputation points
2025-03-15T17:42:58.1666667+00:00
  • Package Name: azure-ai-projects
  • Package Version: Version: 1.0.0b7
  • Operating System: Windows 11
  • Python Version: 3.12

Describe the bug

When using exact sample code at

https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/ai/azure-ai-projects/samples/agents/async_samples/sample_agents_with_file_search_attachment_async.py

also documented at

https://learn.microsoft.com/en-us/python/api/overview/azure/ai-projects-readme?view=azure-python-preview

https://learn.microsoft.com/en-us/azure/ai-services/agents/how-to/tools/file-search?tabs=python&pivots=upload-files-code-examples

substituting only the connection string (which works correctly otherwise) and filepath (to a local .txt, .docx, or .pdf file), the following error is always returned:

raise HttpResponseError(response=response)

azure.core.exceptions.HttpResponseError: (invalidPayload) purpose contains an invalid purpose.

Code: invalidPayload

Message: purpose contains an invalid purpose

The relevant command triggering the error is:

file_obj = await project_client.agents.upload_file_and_poll(

file_path=temp_path,

purpose=FilePurpose.AGENTS

)

However, this is not the case with the non-async code at https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/ai/azure-ai-projects/samples/agents/sample_agents_file_search.py. The issue appears to be async-only.

To Reproduce

Steps to reproduce the behaviour:

  1. Run the script https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/ai/azure-ai-projects/samples/agents/async_samples/sample_agents_with_file_search_attachment_async.py, with appropriate working connection string and file to be uploaded.
  2. I have also tried changing purpose=FilePurpose.AGENTS to purpose="assistants", on basis of the definition of the field at https://learn.microsoft.com/en-us/python/api/azure-ai-projects/azure.ai.projects.operations.agentsoperations?view=azure-python-preview#azure-ai-projects-operations-agentsoperations-upload-file-and-poll, but the error remains the same.

Expected behaviour

I expected the file to be uploaded and polled per the sample script, and per the behaviour with non-async projects.

Azure AI services
Azure AI services
A group of Azure services, SDKs, and APIs designed to make apps more intelligent, engaging, and discoverable.
3,350 questions
{count} votes

Accepted answer
  1. Saideep Anchuri 5,955 Reputation points Microsoft External Staff
    2025-03-19T11:43:33.77+00:00

    Hi Patrick Kelly-Goss

    I think it might be package version specific issue. You might be using outdated packages. I did a minimal repro in my AML studio. Installed the packages " pip install azure-ai-projects azure-identity aiohttp" in compute terminal and run the code after inputting connection string and model deployment name. I am not finding the same error at my side. Could you re-install the packages and let us know after your trial.

    Thank You.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Amira Bedhiafi 31,391 Reputation points
    2025-03-18T21:50:42.9466667+00:00

    Hello Patrick !

    Thank you for posting on Microsoft Learn.

    The value provided for the purpose parameter during the file upload is not recognized or supported by the Azure AI Projects API. In the context of the asynchronous sample code you're using, the purpose parameter should be set to "assistants". This is in line with the official Azure documentation, which specifies that the purpose should be "assistants" when uploading files intended for agent interactions. ​

    Links to help you :

    https://learn.microsoft.com/en-us/python/api/overview/azure/ai-projects-readme?view=azure-python-preview

    https://learn.microsoft.com/en-us/answers/questions/2115697/uploading-fiel-to-azure-openai-with-assistant-purp

    https://stackoverflow.com/questions/79011599/how-to-fix-invalid-value-for-purpose-error-when-using-azure-openai-gpt-4o-min


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.