Skip to content

Fix context references dropped due to wrong isinstance check#1270

Open
jnMetaCode wants to merge 1 commit intokhoj-ai:masterfrom
jnMetaCode:fix/context-isinstance-check-in-chatml
Open

Fix context references dropped due to wrong isinstance check#1270
jnMetaCode wants to merge 1 commit intokhoj-ai:masterfrom
jnMetaCode:fix/context-isinstance-check-in-chatml

Conversation

@jnMetaCode
Copy link
Copy Markdown

Summary

In generate_chatml_messages_with_context(), document references from prior conversation turns are never included in the prompt because of an incorrect isinstance(item, dict) type check.

Bug

chat.context is List[Context] where Context is a Pydantic BaseModel. After ChatMessageModel.model_validate() in the Conversation.messages property, context items are Context objects, not raw dicts. The isinstance(item, dict) guard therefore filters out all items, producing an empty references string every time.

references = "\n\n".join(
    {
        f"# URI: {item.uri or item.file}\n## {item.compiled}\n"
        for item in chat.context or []
        if isinstance(item, dict)   # <-- always False for Context objects
    }
)

Fix

Remove the isinstance(item, dict) guard. The list is already typed as List[Context] and validated by Pydantic, so the type check is unnecessary and harmful.

Impact

Without this fix, the LLM does not see document references from earlier turns in multi-turn conversations, which can degrade response quality when prior context is relevant.

`chat.context` is typed as `List[Context]` where `Context` is a
Pydantic model. The `isinstance(item, dict)` guard filters out all
items since they are `Context` objects after `model_validate()`,
resulting in document references from chat history never being
included in the prompt context.

Remove the incorrect type guard so that note references from prior
conversation turns are properly included.

Signed-off-by: JiangNan <1394485448@qq.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant