본문으로 건너뛰기

[Open WebUI] 채팅 제목 업데이트 시 DB 컨텍스트를 단일 세션으로 통합하여 역직렬화 2회 제거

PR 링크: open-webui/open-webui#23214 상태: Merged | 변경: +13 / -7

들어가며

Open WebUI에서 채팅 제목을 업데이트할 때 update_chat_title_by_id 메서드는 내부적으로 get_chat_by_idupdate_chat_by_id를 연이어 호출했습니다. 각각이 독립적인 DB 컨텍스트를 열어 전체 채팅 JSON을 역직렬화하므로, 제목 하나를 바꾸는데 JSON 역직렬화가 2회 발생했습니다. 이 PR은 단일 DB 컨텍스트에서 직접 업데이트를 수행합니다.

핵심 코드 분석

Before: 두 개의 DB 컨텍스트 체이닝

def update_chat_title_by_id(self, id: str, title: str) -> Optional[ChatModel]:
    chat = self.get_chat_by_id(id)  # DB 컨텍스트 1: 전체 행 SELECT + JSON 역직렬화
    if chat is None:
        return None

    chat = chat.chat
    chat['title'] = title

    return self.update_chat_by_id(id, chat)  # DB 컨텍스트 2: 전체 행 UPDATE + JSON 역직렬화

After: 단일 DB 컨텍스트에서 직접 업데이트

def update_chat_title_by_id(self, id: str, title: str) -> Optional[ChatModel]:
    try:
        with get_db_context() as db:
            chat_item = db.get(Chat, id)
            if chat_item is None:
                return None
            clean_title = self._clean_null_bytes(title)
            chat_item.title = clean_title
            chat_item.chat = {**(chat_item.chat or {}), 'title': clean_title}
            chat_item.updated_at = int(time.time())
            db.commit()
            db.refresh(chat_item)
            return ChatModel.model_validate(chat_item)
    except Exception:
        return None

왜 이게 좋은가

  1. DB 컨텍스트 절반으로 축소: 2개의 독립 세션 대신 1개의 세션으로 동작하므로, 커넥션 획득/반환 오버헤드가 절반으로 줄어든다.
  2. JSON 역직렬화 1회 제거: 기존에는 SELECT 시 전체 chat JSON을 Python dict로 파싱한 후 다시 UPDATE를 위해 직렬화했지만, 이제 chat dict를 직접 수정한다.
  3. null 바이트 정리 보존: _clean_null_bytes를 적용하여 기존의 데이터 정리 로직이 유지된다.

참고 자료

댓글

관련 포스트

PR Analysis 의 다른글