Skip to content
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

response_schema not work for generate_content_stream #14

Closed
hjlarry opened this issue Dec 13, 2024 · 2 comments · Fixed by #96
Closed

response_schema not work for generate_content_stream #14

hjlarry opened this issue Dec 13, 2024 · 2 comments · Fixed by #96
Assignees
Labels
priority: p1 Important issue which blocks shipping the next release. Will be fixed prior to next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.

Comments

@hjlarry
Copy link

hjlarry commented Dec 13, 2024

Thanks for stopping by to let us know something could be better!

PLEASE READ: If you have a support contract with Google, please create an issue in the support console instead of filing on GitHub. This will ensure a timely response.

Is this a client library issue or a product issue? We will only be able to assist with issues that pertain to the behaviors of this library. If the issue you're experiencing is due to the behavior of the product itself, please visit the Support page to reach the most relevant engineers.

If the support paths suggested above still do not result in a resolution, please provide the following details.

Environment details

  • Programming language: Python
  • OS: windows local develop env
  • Language runtime version: python 3.12
  • Package version: google-genai-0.2.2

Steps to reproduce

I follow the tutorial of json_schema, but use the stream mode, will raise error.

This is my code:

response = client.models.generate_content_stream(
    model='gemini-2.0-flash-exp',
    contents='Give me information of the United States.',
    config={
        'response_mime_type': 'application/json',
        'response_schema': {
            'properties': {
                'name': {'type': 'STRING'},
                'population': {'type': 'INTEGER'},
                'capital': {'type': 'STRING'},
                'continent': {'type': 'STRING'},
                'gdp': {'type': 'INTEGER'},
                'official_language': {'type': 'STRING'},
                'total_area_sq_mi': {'type': 'INTEGER'},
            },
            'type': 'OBJECT',
        },
    },
)

for r in response:
    parts = r.candidates[0].content.parts
    for p in parts:
        print(p.text)

This is the error:

Traceback (most recent call last):
  File "C:\Users\hejl\PycharmProjects\dify\api\.venv\Lib\site-packages\google\genai\models.py", line 3723, in generate_content_stream
    return_value = types.GenerateContentResponse._from_response(
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\hejl\PycharmProjects\dify\api\.venv\Lib\site-packages\google\genai\types.py", line 2464, in _from_response
    result.parsed = json.loads(result.text)
                    ^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\hejl\.pyenv\pyenv-win\versions\3.12.0\Lib\json\__init__.py", line 346, in loads
    return _default_decoder.decode(s)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\hejl\.pyenv\pyenv-win\versions\3.12.0\Lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\hejl\.pyenv\pyenv-win\versions\3.12.0\Lib\json\decoder.py", line 353, in raw_decode
    obj, end = self.scan_once(s, idx)
               ^^^^^^^^^^^^^^^^^^^^^^
json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)

if I request with client.models.generate_content it works, but I need the stream, Thanks!

@hjlarry hjlarry added priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns. labels Dec 13, 2024
@sasha-gitg sasha-gitg added priority: p1 Important issue which blocks shipping the next release. Will be fixed prior to next release. and removed priority: p2 Moderately-important priority. Fix may not be included in next release. labels Dec 17, 2024
@sasha-gitg sasha-gitg self-assigned this Dec 19, 2024
@Ark-kun
Copy link

Ark-kun commented Dec 20, 2024

Hello. Thank you for the issue report.

What are your expectation for streaming a JSON object?

When you receive a JSON data chunk like {"prop1": 1, "pro, what are you going to do with this data?

@hjlarry
Copy link
Author

hjlarry commented Dec 20, 2024

Thanks for reply.

I don't require parts of JSON data, but the dify platform use stream mode for all user chat requests, which works with the old gemini sdk. However, it will raise error with the new sdk.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority: p1 Important issue which blocks shipping the next release. Will be fixed prior to next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.
Projects
None yet
3 participants