Skip to content

fix: use RetrievalModel type for retrieval_model field in HitTestingPayload - #33750

Merged
asukaminato0721 merged 2 commits into
langgenius:mainfrom
lyfuci:fix/hit-testing-retrieval-model-pydantic
Mar 19, 2026
Merged

asukaminato0721 merged 2 commits into
langgenius:mainfrom
lyfuci:fix/hit-testing-retrieval-model-pydantic

Conversation

@lyfuci

@lyfuci lyfuci commented Mar 19, 2026

Copy link
Copy Markdown
Contributor

Problem

HitTestingPayload.retrieval_model was typed as dict[str, Any] | None:

class HitTestingPayload(BaseModel):
    query: str = Field(max_length=250)
    retrieval_model: dict[str, Any] | None = None  # ← incorrect
    external_retrieval_model: dict[str, Any] | None = None
    attachment_ids: list[str] | None = None

This caused Flask-RESTX's Swagger documentation generator to emit an incorrect schema for the weights field. Since dict[str, Any] carries no structural information, the generated OpenAPI schema described weights as a primitive float rather than the actual nested WeightModel object:

class WeightModel(BaseModel):
    weight_type: Literal["semantic_first", "keyword_first", "customized"] | None = None
    vector_setting: WeightVectorSetting | None = None
    keyword_setting: WeightKeywordSetting | None = None

Fix

Changed retrieval_model in HitTestingPayload from dict[str, Any] | None to RetrievalModel | None, consistent with DatasetCreatePayload and DatasetUpdatePayload.

The existing parse_args behavior is fully preserved: Pydantic's model_dump(exclude_none=True) correctly serializes the nested RetrievalModel to a dict, so all downstream callers (e.g. HitTestingService.retrieve) receive the same structure as before.

Affected Files

  • api/controllers/console/datasets/hit_testing_base.py

Change Summary

+from services.entities.knowledge_entities.knowledge_entities import RetrievalModel

 class HitTestingPayload(BaseModel):
     query: str = Field(max_length=250)
-    retrieval_model: dict[str, Any] | None = None
+    retrieval_model: RetrievalModel | None = None

…ayload

Previously, retrieval_model was typed as dict[str, Any] | None, which
caused Flask-RESTX to generate incorrect Swagger documentation where the
'weights' field (a nested WeightModel object) was shown as a primitive
float instead of its correct object structure.

Changed to use the structured RetrievalModel Pydantic model, consistent
with DatasetCreatePayload and DatasetUpdatePayload. Pydantic's
model_dump(exclude_none=True) in parse_args correctly serializes the
nested model to dict, so downstream behavior is unchanged.
@lyfuci
lyfuci requested a review from JohnJyong as a code owner March 19, 2026 08:38
@dosubot dosubot Bot added the size:XS This PR changes 0-9 lines, ignoring generated files. label Mar 19, 2026
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request resolves an issue where the OpenAPI (Swagger) documentation for the HitTestingPayload was incorrect due to an overly generic type hint for the retrieval_model field. By replacing the dict[str, Any] type with the specific RetrievalModel class, the schema generation now accurately reflects the structure of nested objects, improving API documentation without altering the existing runtime behavior of the application.

Highlights

  • Corrected retrieval_model type: The retrieval_model field in HitTestingPayload was updated from dict[str, Any] | None to RetrievalModel | None. This change ensures that Flask-RESTX's Swagger documentation generator produces an accurate schema for nested fields, such as weights, which were previously misrepresented as primitive types.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩

@github-actions

Copy link
Copy Markdown
Contributor

Pyrefly Diff

No changes detected.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request correctly updates the retrieval_model field in HitTestingPayload from a generic dict[str, Any] | None to the more specific RetrievalModel | None. This change is crucial for enabling Flask-RESTX's Swagger documentation generator to produce an accurate OpenAPI schema, particularly for nested fields like weights. The addition of the RetrievalModel import is also correctly handled. The change aligns with the stated problem and fix, improving type clarity and API documentation accuracy without altering existing runtime behavior, as model_dump correctly serializes the RetrievalModel to a dictionary for downstream services.

After changing retrieval_model from dict[str, Any] to RetrievalModel,
update test fixtures to supply the required fields (search_method,
reranking_enable, score_threshold_enabled) and use the correct enum
value 'semantic_search' instead of 'semantic'.
@dosubot dosubot Bot added size:S This PR changes 10-29 lines, ignoring generated files. and removed size:XS This PR changes 0-9 lines, ignoring generated files. labels Mar 19, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Pyrefly Diff

No changes detected.

@dosubot dosubot Bot added the lgtm This PR has been approved by a maintainer label Mar 19, 2026
@asukaminato0721
asukaminato0721 merged commit 2b8823f into langgenius:main Mar 19, 2026
14 checks passed
Ssazurov pushed a commit to Ssazurov/dify that referenced this pull request Aug 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

lgtm This PR has been approved by a maintainer size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants