Skip to content

Conversation

@OthmanMohammad
Copy link
Contributor

@OthmanMohammad OthmanMohammad commented Nov 14, 2025

Purpose

Add missing type hints and fix field initialization bug in IntermediateTensors.__init__ method in vllm/sequence.py.

Changes:

  1. Added type hint for tensors parameter: dict[str, torch.Tensor]
  2. Added type hint for kv_connector_output parameter with default value None
  3. Fixed bug where kv_connector_output field was not initialized in custom __init__, which could cause AttributeError at runtime

Changes

  • File: vllm/sequence.py:63-68
  • Added: Type hints for both tensors and kv_connector_output parameters
  • Added: Return type annotation -> None
  • Fixed: Proper initialization of kv_connector_output field in method body

Benefits

  • Improves type safety and IDE autocomplete support
  • Makes parameter types explicit and consistent with class definition
  • Fixes potential runtime bug where kv_connector_output could be accessed before initialization
  • Helps static type checkers catch potential bugs

Test Plan

# Verify Python syntax is valid
python -m py_compile vllm/sequence.py

# Verify the class can be instantiated correctly
python -c "from vllm.sequence import IntermediateTensors; import torch; obj = IntermediateTensors({}); print(obj.kv_connector_output)"

Test Result

✅ File compiles successfully with correct type annotations. ✅ Class instantiates properly with all fields initialized. ✅ kv_connector_output defaults to None when not provided.

Essential Elements of an Effective PR Description Checklist
  • The purpose of the PR, such as "Fix some issue (link existing issues this PR will resolve)".
  • The test plan, such as providing test command.
  • The test results, such as pasting the results comparison before and after, or e2e results
  • (Optional) The necessary documentation update, such as updating supported_models.md and examples for a new model.
  • (Optional) Release notes update. If your change is user facing, please update the release notes draft in the Google Doc.

Signed-off-by: Mohammad Othman <Mo@MohammadOthman.com>
Copy link
Contributor

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

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 adds type hints to the __init__ method of the IntermediateTensors dataclass, which is a good improvement for type safety and code clarity. However, I noticed that the custom __init__ method doesn't initialize the kv_connector_output field defined in the class. This could lead to AttributeError exceptions at runtime. I've provided a suggestion to update the __init__ method to ensure all class fields are properly initialized, making the class more robust.

vllm/sequence.py Outdated
kv_connector_output: KVConnectorOutput | None

def __init__(self, tensors):
def __init__(self, tensors: dict[str, torch.Tensor]) -> None:
Copy link
Contributor

Choose a reason for hiding this comment

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

high

The custom __init__ method does not initialize the kv_connector_output field. Since IntermediateTensors is a dataclass with a custom __init__, it is responsible for initializing all fields that do not have a default value. Currently, kv_connector_output is not initialized upon object creation, which will cause an AttributeError if it's accessed before being explicitly set elsewhere.

To fix this, you should initialize it within __init__, likely with a default value of None. This requires updating both the method signature and its body.

I've suggested a change for the signature below. You will also need to add self.kv_connector_output = kv_connector_output inside the method body to complete the fix.

Suggested change
def __init__(self, tensors: dict[str, torch.Tensor]) -> None:
def __init__(self, tensors: dict[str, torch.Tensor], kv_connector_output: KVConnectorOutput | None = None) -> None:

Signed-off-by: Mohammad Othman <Mo@MohammadOthman.com>
@OthmanMohammad
Copy link
Contributor Author

@gemini-code-assist I've updated the __init__ method to initialize the kv_connector_output field. The signature now includes the parameter with a default value of None, and the field is properly initialized in the method body.

@gemini-code-assist
Copy link
Contributor

Thanks for the update, @OthmanMohammad! I've reviewed the changes to the __init__ method in vllm/sequence.py, and they correctly include the kv_connector_output parameter with a default value of None and proper initialization. This looks good!

@OthmanMohammad OthmanMohammad changed the title Add type hints to IntermediateTensors.__init__ Fix IntermediateTensors initialization and add type hints Nov 14, 2025
Signed-off-by: Mohammad Othman <Mo@MohammadOthman.com>
@DarkLight1337 DarkLight1337 enabled auto-merge (squash) November 15, 2025 02:19
@github-actions github-actions bot added the ready ONLY add when PR is ready to merge/full CI is needed label Nov 15, 2025
@DarkLight1337 DarkLight1337 merged commit 363aaee into vllm-project:main Nov 15, 2025
43 checks passed
geodavic pushed a commit to geodavic/vllm that referenced this pull request Nov 16, 2025
…ct#28743)

Signed-off-by: Mohammad Othman <Mo@MohammadOthman.com>
Co-authored-by: Mohammad Othman <Mo@MohammadOthman.com>
Signed-off-by: George D. Torres <gdavtor@gmail.com>
bwasti pushed a commit to bwasti/vllm that referenced this pull request Nov 17, 2025
…ct#28743)

Signed-off-by: Mohammad Othman <Mo@MohammadOthman.com>
Co-authored-by: Mohammad Othman <Mo@MohammadOthman.com>
Signed-off-by: Bram Wasti <bwasti@meta.com>
bringlein pushed a commit to bringlein/vllm that referenced this pull request Nov 26, 2025
…ct#28743)

Signed-off-by: Mohammad Othman <Mo@MohammadOthman.com>
Co-authored-by: Mohammad Othman <Mo@MohammadOthman.com>
devpatelio pushed a commit to SumanthRH/vllm that referenced this pull request Nov 29, 2025
…ct#28743)

Signed-off-by: Mohammad Othman <Mo@MohammadOthman.com>
Co-authored-by: Mohammad Othman <Mo@MohammadOthman.com>
kitaekatt pushed a commit to kitaekatt/vllm that referenced this pull request Dec 1, 2025
…ct#28743)

Signed-off-by: Mohammad Othman <Mo@MohammadOthman.com>
Co-authored-by: Mohammad Othman <Mo@MohammadOthman.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready ONLY add when PR is ready to merge/full CI is needed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants