LCORE-1350: Increased type-safety of tool utilities#1229
LCORE-1350: Increased type-safety of tool utilities#1229tisnik merged 1 commit intolightspeed-core:mainfrom
Conversation
WalkthroughThis PR refactors the type system for response handling and tools management. It introduces structured type definitions (InputTool, InputToolFileSearch, InputToolMCP) to replace dict-based tool representations, expands ResponsesApiParams with new fields for enhanced response composition, standardizes Optional type annotations throughout responses utilities, and adds explicit string casting and None-field filtering in endpoint response handlers. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
Suggested reviewers
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
| try: | ||
| moderation_result = await run_shield_moderation( | ||
| client, responses_params.input, shield_ids | ||
| client, cast(str, responses_params.input), shield_ids |
There was a problem hiding this comment.
Query endpoints always use string-like inputs so it is safe to cast.
| response = await client.responses.create(**responses_params.model_dump()) | ||
| response = await client.responses.create( | ||
| **responses_params.model_dump(exclude_none=True) | ||
| ) |
There was a problem hiding this comment.
Explicitly drop unset attributes (queries use reduced set of responses attributes).
| return tool.get("vector_store_ids", []) | ||
| return [] | ||
| if tool.type == "file_search": | ||
| vector_store_ids.extend(tool.vector_store_ids) |
There was a problem hiding this comment.
Minor functional change - solves the case when there are multiple file_search objects.
There was a problem hiding this comment.
Thanks, it might be relevant in the future! 👍
| tools.append(tool_def) | ||
| authorization = headers.pop("Authorization", None) | ||
| tools.append( | ||
| InputToolMCP( |
There was a problem hiding this comment.
Creates the whole object at once
| tool_results: list[dict[str, Any]] = Field(default_factory=list) | ||
|
|
||
|
|
||
| ResponseItem: TypeAlias = ( |
There was a problem hiding this comment.
Moved at the top of the module
| ResponseInput: TypeAlias = str | list[ResponseItem] | ||
|
|
||
|
|
||
| class ResponsesApiParams(BaseModel): |
There was a problem hiding this comment.
Extended model with all currently available attributes in LLS.
There was a problem hiding this comment.
Actionable comments posted: 3
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@src/app/endpoints/query.py`:
- Around line 296-305: Replace the unsafe typing cast usage by converting
structured input to a string before passing it to moderation and conversation
functions: call content_to_str(responses_params.input) and use that returned
string in run_shield_moderation(...) and append_turn_to_conversation(...).
Update the calls where cast(str, responses_params.input) appears (refer to
responses_params.input, run_shield_moderation, and append_turn_to_conversation)
so they pass the content_to_str(...) result instead, ensuring you import
content_to_str from src.utils.types if not already imported.
In `@src/app/endpoints/streaming_query.py`:
- Around line 287-299: Replace the unsafe type-only cast of
responses_params.input with a runtime coercion using content_to_str from
utils.types: import content_to_str and use
content_to_str(responses_params.input) when calling run_shield_moderation and
append_turn_to_conversation (locations involving run_shield_moderation,
moderation_result, append_turn_to_conversation and responses_params.input) so
both string and list[ResponseItem] inputs are handled correctly at runtime;
mirror the same change in the similar patterns found in
src/app/endpoints/query.py.
In `@src/utils/responses.py`:
- Around line 298-299: The docstring for the parameter "tools" still mentions
"typed InputTool or dict" which is stale; update the docstring to state that
"tools" is a sequence/list of typed InputTool instances (or the exact type used
in this module) and remove any mention of dict so it reflects the typed-only API
(referencing the "tools" parameter and the InputTool type in the docstring).
ℹ️ Review info
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (6)
src/app/endpoints/query.pysrc/app/endpoints/streaming_query.pysrc/utils/query.pysrc/utils/responses.pysrc/utils/types.pytests/unit/utils/test_responses.py
tisnik
left a comment
There was a problem hiding this comment.
LGTM, indeed, now it's more readable
| tool.server_url: tool.headers | ||
| for tool in tools | ||
| if tool.get("type") == "mcp" and tool.get("headers") and tool.get("server_url") | ||
| if tool.type == "mcp" and tool.headers |
There was a problem hiding this comment.
Do we have assurance that server_url won't be e.g. "" or None (I think InputToolMCP(server_label="x", server_url="") would be valid but we don't want this)? If not, better bring back and tool.server_url.
|
|
||
| Parameters: | ||
| tools: The prepared tools list from ResponsesApiParams. | ||
| tools: The prepared tools list (typed InputTool or dict). |
There was a problem hiding this comment.
Given the change you've made, this should be more accurate?
The prepared tools list of InputTool objects.
Description
This PR refactors:
Type of change
Tools used to create PR
Identify any AI code assistants used in this PR (for transparency and review context)
Related Tickets & Documents
Checklist before requesting a review
Testing
Summary by CodeRabbit
New Features
Bug Fixes
Refactor