Skip to content

Conversation

@ashwinb
Copy link
Contributor

@ashwinb ashwinb commented Oct 28, 2025

Summary

Fixes 111 mypy type errors in OpenAI compatibility layer (PR3 in mypy remediation series).

Changes:

  • litellm_openai_mixin.py: Added type annotations, None checks for tool_config/model_store access
  • openai_compat.py: Added None checks throughout, fixed TypedDict expansions, proper type conversions for messages/tool_calls

Result: 23 → 1 errors in litellm file, 88 → 0 errors in openai_compat file

@meta-cla meta-cla bot added the CLA Signed This label is managed by the Meta Open Source bot. label Oct 28, 2025
@ashwinb ashwinb changed the title fix(mypy): resolve OpenAI compatibility layer type issues fix(mypy): resolve OpenAI compatibility layer type issues (part 02) Oct 28, 2025
@ashwinb ashwinb changed the title fix(mypy): resolve OpenAI compatibility layer type issues (part 02) fix(mypy): part-02 resolve OpenAI compatibility layer type issues Oct 28, 2025
@ashwinb ashwinb changed the base branch from main to mypy/01-meta-reference-agents October 28, 2025 20:10
@ashwinb ashwinb force-pushed the mypy/01-meta-reference-agents branch from 6822dec to 2882ae3 Compare October 28, 2025 20:12
ashwinb and others added 2 commits October 28, 2025 13:12
Resolves 111 mypy errors across OpenAI inference compatibility utilities:

litellm_openai_mixin.py (23 errors → 1 unavoidable):
- Add type annotation for input_dict parameter
- Fix JsonSchemaResponseFormat dict conversion and manipulation
- Add None checks for tool_config access with walrus operator
- Fix get_api_key() to properly handle None key_field
- Add model_store None checks in all three OpenAI methods
- Add type annotations for provider_resource_id variable
- Add type: ignore for litellm external library returns

openai_compat.py (88 errors → 0):
- Add None checks for TopPSamplingStrategy temperature and top_p
- Add type: ignore for no-any-return in text_from_choice
- Fix union-attr errors in logprobs iteration with None checks
- Add None checks for choice.text and finish_reason
- Fix OpenAICompatCompletionChoice.message attribute access
- Filter tool_calls to only valid ToolCall objects
- Add type annotations for converted_messages and choices lists
- Fix TypedDict ** expansion issues in message conversions
- Add type: ignore for function dict index operations
- Fix tool_choice and response_format type conversions
- Add type annotations for lls_tools variable
- Fix sampling strategy assignment with proper ordering
- Add None checks for buffer string concatenations
- Rename shadowed tool_call variable to parsed_tool_call
- Fix message content and tool_calls type conversions
- Add isinstance checks before attribute access on content
- Fix OpenAI finish_reason type literal conversions

Code remains clean and readable with strategic use of type: ignore
pragmas only where necessary for external library compatibility.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
@ashwinb ashwinb force-pushed the mypy/02-openai-compat-layer branch from 6d3f196 to a8d51a1 Compare October 28, 2025 20:14
ashwinb and others added 4 commits October 28, 2025 13:49
- Simplify provider_resource_id assignment with assertion (review comment 1)
- Fix comment placement order (review comment 2)
- Refactor tool_calls list building to avoid union-attr suppression (review comment 3)
- Rename response_format to response_format_dict to avoid shadowing (review comment 4)
- Update type: ignore comments for message.content with accurate explanation of OpenAI SDK type alias resolution issue (review comment 5)
- Add assertions in litellm_openai_mixin to validate provider_resource_id is not None

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
Changed comment from "message.content uses list[AliasType] but mypy expects Iterable[BaseType]" to "OpenAI SDK uses aliased types internally that mypy sees as incompatible with base types".

This is more accurate - the OpenAI SDK's message parameter types use aliased names (like OpenAIChatCompletionContentPartTextParam) internally in their type annotations, and mypy cannot match these with base type names (ChatCompletionContentPartTextParam) even though they're the same types at runtime.

Verified that importing and using base types directly doesn't resolve the issue because the SDK's internal type annotations still use the aliased names.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants