alibaba/spring-ai-alibaba · info
会话 {} 不存在,创建新会话
Error message
会话 {} 不存在,创建新会话 What it means
In PromptRunServiceImpl.getOrCreateSession, when chatSessionService.getSession(sessionId) returns null, this warning is logged and a brand-new session is created via createSessionWithMockTools instead of failing. The client-supplied session id does not reference an existing session, so a fresh one is silently created.
Source
Thrown at spring-ai-alibaba-admin/spring-ai-alibaba-admin-server-start/src/main/java/com/alibaba/cloud/ai/studio/admin/service/impl/PromptRunServiceImpl.java:133
/**
* 获取或创建会话
*/
private ChatSession getOrCreateSession(PromptRunRequest request) {
// 如果强制创建新会话或没有提供sessionId,创建新会话
if (Boolean.TRUE.equals(request.getNewSession()) || request.getSessionId() == null || request.getSessionId()
.trim().isEmpty()) {
return chatSessionService.createSessionWithMockTools(request.getPromptKey(), request.getVersion(),
request.getTemplate(), request.getVariables(), request.getModelConfig(), request.getMockTools());
}
// 尝试获取现有会话
ChatSession existingSession = chatSessionService.getSession(request.getSessionId());
if (existingSession != null) {
return existingSession;
}
// 会话不存在,创建新会话
log.warn("会话 {} 不存在,创建新会话", request.getSessionId());
return chatSessionService.createSessionWithMockTools(request.getPromptKey(), request.getVersion(),
request.getTemplate(), request.getVariables(), request.getModelConfig(), request.getMockTools());
}
/**
* 生成真实的AI流式响应
*
* @param session 会话对象
* @return 流式响应
*/
private Flux<PromptRunResponse> generateRealAIResponse(ChatSession session, PromptRunRequest request) {
// 用于收集完整响应的容器
AtomicReference<StringBuilder> completeResponse = new AtomicReference<>(new StringBuilder());
AtomicReference<ChatMessageMetrics> metrics = new AtomicReference<>(ChatMessageMetrics.builder().build());
String fullPrompt = modelConfigParser.replaceVariables(session.getTemplate(), session.getVariables());
Map<String, String> observationMetadata = new HashMap<>(4);
if (StringUtils.hasText(request.getPromptKey()) && !"playground".equals(request.getPromptKey())) {View on GitHub (pinned to f82da0b50f)
Solutions
- Create the session first via the session API and use the returned id in subsequent prompt-run calls
- Persist and reuse the session id returned by createSession on the client side
- If silent recreation is undesired, check session existence before running and surface an error instead
- Verify the session store backend still holds the session (restarts may clear in-memory sessions)
Example fix
// before
request.setSessionId("stale-id"); // may silently create a new session
runPrompt(request);
// after
ChatSession s = chatSessionService.getSession(sessionId);
if (s == null) { s = chatSessionService.createSession(...); sessionId = s.getId(); }
request.setSessionId(sessionId); Defensive patterns
Strategy: validation
Validate before calling
ChatSession s = chatSessionService.getSession(request.getSessionId()); if (s == null) { s = createSession(); request.setSessionId(s.getId()); } Prevention
- Always create a session first and reuse the returned id
- Persist session ids client-side across requests
- Remember server restarts/TTL can invalidate sessions
When it happens
Trigger: Calling the prompt run/stream API with a sessionId that was never created or has expired/been deleted.
Common situations: Client caching a stale session id after server restart or session TTL expiry; typos in the session id; tests/integration runs reusing ids from a cleaned database.
Understand the failure class
Background: Record Not Found Errors: "not found", RecordNotFound, and "was not found" — what they mean and how to fix them — this error's family across 28 libraries.
Related errors
- 会话不存在:
- Less than 2 messages in state when last message is ToolRespo
- Shell session not initialized. Cannot restart a session that
- Thread '%s' not found or already released
- Cannot append to non-list value for key: {item.targetKey()}
AI-assisted analysis of alibaba/spring-ai-alibaba@f82da0b50f (2026-09-09).
Data as JSON: /api/errors/97ec38ae38cfd0d6.
Report an issue: GitHub.