Access Azure AI Foundry threads & messages via Azure Python SDK

桂學文 Kevin Kuei 125 Reputation points
2025-06-11T10:27:51.98+00:00

Hi,

I understand that Azure AI Foundry stores agent threads and messages. I would like to ask if there is a Python API available that allows me to retrieve the threads and their corresponding messages? That way, I can access the previous messages and continue the conversation from where it left off.

Regards,

Kevin Kuei

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

Accepted answer
  1. JAYA SHANKAR G S 4,035 Reputation points Microsoft External Staff Moderator
    2025-06-12T09:21:50.9533333+00:00

    Hello @桂學文 Kevin Kuei ,

    Yes, you can get the agents, threads and messages using python sdk.

    Check this documentation where you can use azure-ai-agents or azure-ai-projects package.

    Below is the sample code using azure-ai-projects package with AIProjectsClient.

    from azure.ai.projects import AIProjectClient
    from azure.identity import DefaultAzureCredential
    
    project_client = AIProjectClient(endpoint="Foundry-based-project-endpoint", credential=DefaultAzureCredential())
    
    agents = list(project_client.agents.list_agents())
    threads = list(project_client.agents.threads.list())
    messages = list(project_client.agents.messages.list(threads[0].id))
    
    

    Above code gets agents, threads and messages based on thread id.

    user_messages = [msg for msg in messages if msg.get("role") == "user"]
    assistant_messages = [msg for msg in messages if msg.get("role") == "assistant"]
    
    print("User Messages:")
    for msg in user_messages:
        print(msg.content)
    
    print("\nAssistant Messages:")
    for msg in assistant_messages:
        print(msg.content)
    

    Output:

    enter image description here

    Customize the above code for you use case.

    If the solution helped you do accept and give feedback clicking on yes.

    Thank you

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

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.