Skip to content

[Bug]: search() with a custom response_model raises ValidationError on SearchResultPayload (and error logging hangs) #3048

@vkovac2

Description

@vkovac2

Bug Description

cognee.search(..., retriever_specific_config={"response_model": MyModel}) fails after retrieval succeeds. The retriever
returns a Pydantic MyModel instance, but SearchResultPayload.completion is typed Optional[Union[str, List[str], List[dict]]], which rejects a model instance → ValidationError when the payload is built at
cognee/modules/search/methods/get_retriever_output.py:80.

Two problems:

  1. The response_model search path can't return a structured object — the payload type doesn't accept a BaseModel.
  2. When that ValidationError is logged, cognee's structlog → rich exception renderer recurses in rich/pretty._traverse
    and hangs indefinitely, so instead of a fast error the call appears to deadlock.

Steps to Reproduce

Minimal (no graph/LLM needed — it's pure result packaging):

from cognee.modules.search.models.SearchResultPayload import SearchResultPayload
from cognee.modules.search.types.SearchType import SearchType
from pydantic import BaseModel

class DealBrief(BaseModel):
    deal_name: str = ""
    health: str = ""

SearchResultPayload(completion=DealBrief(deal_name="Acme"), search_type=SearchType.GRAPH_COMPLETION)

End-to-end:

await cognee.search(
    query_text="Brief for the Acme deal",
    query_type=SearchType.GRAPH_COMPLETION,
    datasets="my_dataset",
    retriever_specific_config={"response_model": DealBrief},
)

Expected Behavior

search() with a response_model returns the populated model instance (schema-conforming structured output). The completion
retrievers already accept response_model via retriever_specific_config, and generate_completion returns a model
instance — only the result payload can't carry it.

Actual Behavior

pydantic ValidationError: N validation errors for SearchResultPayload at get_retriever_output.py:80:

completion.str: Input should be a valid string [input_type=DealBrief]
completion.list[str].0 / list[dict[any,any]].0: ...

Separately, logging that exception hangs indefinitely in rich/pretty._traverse (cognee/shared/logging_utils.py
structlog → rich traceback), so the call never returns and looks like a deadlock.

Environment

Environment

  • OS: macOS (Darwin 25.3.0, arm64)
  • Python: 3.12.13
  • Cognee: 1.1.2 (installed from dev)
  • LLM Provider: Baseten (gpt-oss-120b) + OpenAI embeddings
  • Database: default local Kuzu/Ladybug graph + LanceDB
  • Note: the failure is store-independent — it's in post-retrieval result packaging, so it reproduces on any backend.

Logs/Error Messages

Additional Context

Root cause: SearchResultPayload.completion (in cognee/modules/search/models/SearchResultPayload.py) is
Optional[Union[str, List[str], List[dict]]] — no BaseModel, and no serializer (whereas result_object has a
field_serializer for complex types).

Suggested fix: widen completion to accept a model (e.g. add BaseModel to the Union) and model_dump() it on output;
and harden the exception logger so rich traceback rendering can't hang on the error object.

Pre-submission Checklist

  • I have searched existing issues to ensure this bug hasn't been reported already
  • I have provided a clear and detailed description of the bug
  • I have included steps to reproduce the issue
  • I have included my environment details

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No fields configured for Bug.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions