Skip to content

[Bug]: KnowledgePoint validation rejects LLM-extracted data (prerequisites & document_id) #497

Description

@jes614753-sketch

Problem

KnowledgePoint model has two validation issues when used with LLM-extracted knowledge points:

  1. document_id is required — LLM extraction doesn't produce this field, causing Field required [type=missing] error
  2. prerequisites expects UUID list — LLM outputs prerequisite titles as strings (e.g., ["智能体的定义"]), causing uuid_parsing error

Steps to Reproduce

  1. Extract knowledge points from a document using KnowledgeExtractorAgent
  2. Save results to knowledge_points.json
  3. Load and validate with KnowledgePoint.model_validate(kp)
  4. Pydantic raises validation errors

Expected

The model should be flexible enough to handle LLM-generated data.

Suggested Fix

  1. Make document_id optional:
document_id: UUID | None = None
  1. Add a validator to coerce string prerequisites to UUIDs:
@field_validator("prerequisites", mode="before")
@classmethod
def coerce_prerequisites(cls, v: list) -> list[UUID]:
    result = []
    for item in v:
        if isinstance(item, UUID):
            result.append(item)
        elif isinstance(item, str):
            try:
                result.append(UUID(item))
            except ValueError:
                result.append(uuid5(NAMESPACE_DNS, item))
    return result

Environment

  • Python 3.14, Windows 11, latest main branch

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions