Skip to content

Incorrect type hint for chunking_strategy params on File class methods #2005

Closed
@evangriffiths

Description

@evangriffiths

Confirm this is an issue with the Python library and not an underlying OpenAI API

  • This is an issue with the Python library

Describe the bug

If I pass in a StaticFileChunkingStrategyParam to OpenAI.beta.vector_stores.create I get:

openai.BadRequestError: Error code: 400 - {'error': {'message': "Missing required parameter: 'chunking_strategy.type'.", 'type': 'invalid_request_error', 'param': 'chunking_strategy.type', 'code': 'missing_required_parameter'}}

To Reproduce

Run this repro:

import tempfile

from openai import OpenAI
from openai.types.beta.static_file_chunking_strategy_object import (
    StaticFileChunkingStrategyObject,
)
from openai.types.beta.static_file_chunking_strategy_param import (
    StaticFileChunkingStrategyParam,
)

client = OpenAI()
with tempfile.NamedTemporaryFile(suffix=".txt", mode="w+b") as temp_file:
    temp_file.write(b"foo bar")
    temp_file.flush()
    temp_file.seek(0)
    file = client.files.create(file=temp_file.file, purpose="assistants")

vector_store = client.beta.vector_stores.create(name="foo")
client.beta.vector_stores.files.create(
    vector_store_id=vector_store.id,
    file_id=file.id,
    chunking_strategy=StaticFileChunkingStrategyParam(
        max_chunk_size_tokens=250,
        chunk_overlap_tokens=10,
    ),
)

Based on type hints I'd expect this to work, but I get the above api error.

It works instead with:

from openai.types.beta.static_file_chunking_strategy_object import (
    StaticFileChunkingStrategyObject,
)
...
client.beta.vector_stores.files.create(
    vector_store_id=vector_store.id,
    file_id=file.id,
    chunking_strategy=StaticFileChunkingStrategyObject(
        type="static",
        static=StaticFileChunkingStrategyParam(
            max_chunk_size_tokens=250,
            chunk_overlap_tokens=10,
        ),
    ),
)

i.e. looks the type hint should be changed to FileChunkingStrategy. Note there are many such methods with this param in Files (e.g. create_and_poll, upload, ...). I haven't tested these, but this change may apply to them too.

Code snippets

No response

OS

macos 14.3.1

Python version

Python 3.10.14

Library version

openai==1.59.6

Activity

Programmer-RD-AI

Programmer-RD-AI commented on Jan 14, 2025

@Programmer-RD-AI

Hi,

Thanks for bringing up this issue and providing such detailed information! 🙌 After investigating the problem further, I believe I found the root cause and made the following changes to resolve it.

Issue:

The current FileChunkingStrategyParam type hint in openai/types/beta/file_chunking_strategy_param.py includes:

FileChunkingStrategyParam: TypeAlias = Union[AutoFileChunkingStrategyParam, StaticFileChunkingStrategyParam]

However, as the API error suggests, StaticFileChunkingStrategyParam should be encapsulated in a StaticFileChunkingStrategyObject. This mismatch is why passing StaticFileChunkingStrategyParam directly results in a 400 - Missing required parameter: 'chunking_strategy.type' error.


Fix:

I updated the type hint to use StaticFileChunkingStrategyObject instead of StaticFileChunkingStrategyParam:

FileChunkingStrategyParam: TypeAlias = Union[AutoFileChunkingStrategyParam, StaticFileChunkingStrategyObject]

This change reflects the correct structure expected by the API and aligns the type hints with actual usage. Additionally, this resolves the error when using StaticFileChunkingStrategyParam.


Concern:

While this change resolves the immediate issue, I noticed that AutoFileChunkingStrategyParam doesn't include a static field, which StaticFileChunkingStrategyObject does. This inconsistency might cause issues if similar expectations arise for the AutoFileChunkingStrategyParam in the future. I plan to explore this further to ensure compatibility across the board.


Next Steps:

Thanks again for bringing this up, and I hope this update helps! Let me know if there’s anything else I should explore or test.

RobertCraigie

RobertCraigie commented on Jan 14, 2025

@RobertCraigie
Collaborator

Thanks for the report! This will be fixed in the next release
#2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Incorrect type hint for `chunking_strategy` params on `File` class methods · Issue #2005 · openai/openai-python