Skip to content

[Bug]: Type hint error because of improper field definition #18856

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
medihack opened this issue May 26, 2025 · 2 comments
Open

[Bug]: Type hint error because of improper field definition #18856

medihack opened this issue May 26, 2025 · 2 comments
Labels
bug Something isn't working P2

Comments

@medihack
Copy link

Bug Description

When using OpenAILike type checker like pyright argue about missing max_tokens and api_version parameters. This is not the case when OpenAI (which OpenAILike inherits from).
The reason is that for example api_version field is not optional and does not have a default value, but is indirectly set as optional in the constructor. This unfortunately confuses the type checker. Why not make both fields optional and give them a default?

Version

llama-index-llms-openai-like v0.3.5, lama-index-llms-openai v0.3.42

Steps to Reproduce

When using

OpenAILike(
    model="my_model",
    api_key="foobar",
    api_base="https//whatever/v1",
    temperature=0.0,
)

pyright tells me max_tokens and api_version is missing.

Relevant Logs/Tracbacks

@medihack medihack added bug Something isn't working triage Issue needs to be triaged/prioritized labels May 26, 2025
Copy link

dosubot bot commented May 26, 2025

The type hint error you're seeing with OpenAILike and missing max_tokens and api_version parameters is likely due to a mismatch between how these fields are defined in the class and how type checkers like pyright interpret them.

In the current implementation, both max_tokens and api_version are defined as optional parameters with default values of None in the constructor of OpenAILike—so, in theory, they should not be required when you instantiate the class. However, if these fields are also defined as class attributes (for example, using dataclasses or Pydantic fields) without default values or without being marked as Optional, type checkers may still consider them required, leading to the error you described. This can happen if the field definition and the constructor signature are out of sync, which confuses static analysis tools.

For reference, the constructor for OpenAILike does define both parameters as optional with defaults:

def __init__(
    ...,
    max_tokens: Optional[int] = None,
    ...,
    api_version: Optional[str] = None,
    ...
)

source

However, if the class attribute for api_version (or max_tokens) is not marked as optional and does not have a default value, this can cause the type checker to require them at instantiation time, even though the constructor does not.

Making both fields explicitly optional and giving them default values both in the class attribute definition and the constructor should resolve the type checker confusion. This approach is consistent with how similar parameters are handled in related classes, such as OpenAILikeEmbedding and OpenAIEmbedding, where optional parameters are given default values and passed through the constructor chain as needed
source.

To summarize:

  • The error is due to a mismatch between field definitions and constructor signatures.
  • Making max_tokens and api_version optional with default values in both the class attribute and constructor should resolve the issue for type checkers like pyright.
  • There are no existing issues or discussions in the repository about this specific problem.

If you have control over the code, updating the class definition as described should fix the type hint errors. If not, you may want to raise this as a bug or feature request with the maintainers.

To reply, just mention my name @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other  Join Discord Share on X

@logan-markewich
Copy link
Collaborator

PR very welcome @medihack

@logan-markewich logan-markewich added P2 and removed triage Issue needs to be triaged/prioritized labels May 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working P2
Projects
Status: No status
Development

No branches or pull requests

2 participants