Skip to content

[bug] instructor max retries invocation params issue #1569

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
peterhollis opened this issue Apr 28, 2025 · 1 comment
Open

[bug] instructor max retries invocation params issue #1569

peterhollis opened this issue Apr 28, 2025 · 1 comment
Assignees
Labels
bug Something isn't working language: python Related to Python integration

Comments

@peterhollis
Copy link

peterhollis commented Apr 28, 2025

Describe the bug
When max_retries is set as a Tenacity Retrying object, Type Error: Object of type Retrying is not JSON serializable is raised by _wrapper.py (line 160)

To Reproduce
Assign max_retries as a Retrying object as seen here in the Instructor docs.

Expected behavior
Retrying object to be excluded from SpanAttributes.LLM_INVOCATION_PARAMETERS so the Type Error is not raised.

Desktop (please complete the following information):

  • OS: MacOS
  • Version 15.1.1
  • Python: 3.13

Additional context

Example code:

import instructor
from openai import OpenAI
from pydantic import BaseModel
from tenacity import Retrying, stop_after_attempt, wait_fixed

client = instructor.from_openai(OpenAI())

class UserInfo(BaseModel):
    name: str
    age: int

response = client.chat.completions.create(
    model="gpt-4o-mini",
    response_model=UserInfo,
    messages=[
        {"role": "user", "content": "John Doe is 30 years old."},
    ],
    max_retries=Retrying(
        stop=stop_after_attempt(2), 
        wait=wait_fixed(1), 
    )
)
@peterhollis peterhollis added bug Something isn't working triage Issues that require triage labels Apr 28, 2025
Copy link

dosubot bot commented Apr 29, 2025

Hello @peterhollis! I'm Dosu, your friendly bot here to help you tackle bugs, answer questions, and support your journey to becoming a contributor. I'll be around while you wait for a human maintainer. How can I assist you today?

The issue you're encountering is due to the Retrying object from Tenacity not being JSON serializable, which causes a TypeError when it's included in SpanAttributes.LLM_INVOCATION_PARAMETERS. The _wrapper.py file currently does not have a mechanism to exclude specific types like Retrying from serialization [1].

To resolve this, you could modify the serialization logic to exclude or handle the Retrying object specifically. One approach is to update the safe_json_dumps function or the SafeJSONEncoder class to check for instances of Retrying and handle them accordingly, such as by converting them to a string representation or excluding them from the serialization process. This would prevent the TypeError from being raised.

To continue talking to Dosu, mention @dosu.


Help Dosu learn! Give it feedback: Great Response | Irrelevant Answer | Incorrect Sources | Too Verbose | Hallucination | Bug Report | Other

@dosubot dosubot bot added the language: python Related to Python integration label Apr 29, 2025
@caroger caroger removed the triage Issues that require triage label Apr 29, 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 language: python Related to Python integration
Projects
Status: No status
Development

No branches or pull requests

3 participants