Use user profiles
Updated at:
Create profile schemas, extract profiles, and retrieve user profiles via API
User profiles extract structured user attributes (e.g., age, occupation, preferences) from conversations. For console configuration, see Configure memory rules.
This page covers the full API workflow: create a profile schema, extract from a conversation, and retrieve the profile.
Prerequisites
- A memory library has been created, or use the default memory library
DASHSCOPE_API_KEYconfigured- Replace
{WorkspaceId}in the request URLs below with your actual workspace ID. For the Python SDK, setMEMORY_SERVICE_ENDPOINT. See Service address
Full workflow
Create schema → Add conversation and extract → Get user profile.
# 1. Create profile schema (CreateProfileSchema)
curl -X POST https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api/v2/apps/memory/profile_schemas \
--header "Authorization: Bearer $DASHSCOPE_API_KEY" \
--header "Content-Type: application/json" \
--data '{
"name": "User basic profile",
"description": "User info with age and interests",
"extract_scene": "efficient",
"attributes": [
{"name": "Age", "description": "User age"},
{"name": "Hobby", "description": "User hobbies"},
{"name": "Occupation", "description": "User occupation"}
]
}'
# 2. Add conversation and extract profile (AddMemory with profile_schema_id)
curl -X POST https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api/v2/apps/memory/add \
--header "Authorization: Bearer $DASHSCOPE_API_KEY" \
--header "Content-Type: application/json" \
--data '{
"user_id": "user_001",
"messages": [
{"role": "user", "content": "I am 28, a software engineer. I play football on weekends."},
{"role": "assistant", "content": "Nice to meet you!"}
],
"profile_schema": "YOUR_SCHEMA_ID"
}'
# 3. Get user profile (GetUserProfile, wait 3s first)
curl -X GET "https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api/v2/apps/memory/profile_schemas/{YOUR_SCHEMA_ID}/user_profile?user_id=user_001" \
--header "Authorization: Bearer $DASHSCOPE_API_KEY" \
--header "Content-Type: application/json"
from agentscope_runtime.tools.modelstudio_memory import (
CreateProfileSchema, GetUserProfile, AddMemory,
ProfileAttribute, CreateProfileSchemaInput,
GetUserProfileInput, AddMemoryInput, Message,
)
import asyncio
async def profile_example():
create_schema = CreateProfileSchema()
get_profile = GetUserProfile()
add_memory = AddMemory()
try:
# 1. Create profile schema
schema_result = await create_schema.arun(CreateProfileSchemaInput(
name="User basic profile",
description="User info with age and interests",
attributes=[
ProfileAttribute(name="Age", description="User age"),
ProfileAttribute(name="Hobby", description="User hobbies"),
ProfileAttribute(name="Occupation", description="User occupation"),
]
))
schema_id = schema_result.profile_schema_id
# 2. Add conversation and extract (profile_schema is required, otherwise no profile is extracted)
await add_memory.arun(AddMemoryInput(
user_id="user_001",
profile_schema=schema_id,
messages=[
Message(role="user", content="I am 28, a software engineer. I play football on weekends."),
Message(role="assistant", content="Nice to meet you!"),
]
))
await asyncio.sleep(3) # wait for extraction
# 3. Get user profile
profile = await get_profile.arun(GetUserProfileInput(
schema_id=schema_id, user_id="user_001"
))
for attr in profile.profile.attributes:
print(f"{attr.name}: {attr.value or 'not extracted'}")
finally:
await create_schema.close()
await get_profile.close()
await add_memory.close()
asyncio.run(profile_example())
NoteProfile extraction is asynchronous. After adding a conversation, the first query may not contain attribute values, so retry according to your application's retry policy.
ImportantAfter profiles are set up, continue to manage memories to view and search existing memories.
Is this page helpful?