Skip to content

Conversation

edge7
Copy link

@edge7 edge7 commented Jul 22, 2025

this simple code was failing for me:

import instructor
from pydantic import BaseModel

MODEL = "openai/o3"
client = instructor.from_provider(MODEL)


class Name(BaseModel):
    name: str


(completion, raw) = client.chat.completions.create_with_completion(
    model=MODEL.split("/")[-1],
    messages=[
        {"role": "system", "content": "extract info when possible"},
        {"content": "my name is Enrico", "role": "user"},
    ],
    response_model=Name,
    max_retries=3,
)
print(raw.usage)

Important

Adds _raw_response to AdapterBase models in process_response_async and process_response to ensure raw response accessibility.

  • Behavior:
    • Adds _raw_response attribute to model.content for AdapterBase instances in process_response_async and process_response functions.
    • Ensures raw response is accessible for AdapterBase models, allowing access to metadata like token usage.
  • Functions:
    • Modifies process_response_async and process_response in response.py to attach _raw_response to model.content for AdapterBase.
  • Logging:
    • Adds debug logging for returning models from AdapterBase in both functions.

This description was created by Ellipsis for c6221a1. You can customize this summary. It will automatically update as commits are pushed.

this simple code was failing for me:

import instructor
from pydantic import BaseModel

MODEL = "openai/o3"
client = instructor.from_provider(MODEL)


class Name(BaseModel):
    name: str


(completion, raw) = client.chat.completions.create_with_completion(
    model=MODEL.split("/")[-1],
    messages=[
        {"role": "system", "content": "extract info when possible"},
        {"content": "my name is Enrico", "role": "user"},
    ],
    response_model=Name,
    max_retries=3,
)
print(raw.usage)
Copy link
Contributor

@ellipsis-dev ellipsis-dev bot left a comment

Choose a reason for hiding this comment

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

Important

Looks good to me! 👍

Reviewed everything up to c6221a1 in 1 minute and 41 seconds. Click for details.
  • Reviewed 20 lines of code in 1 files
  • Skipped 0 files when reviewing.
  • Skipped posting 2 draft comments. View those below.
  • Modify your settings and rules to customize what types of comments Ellipsis leaves. And don't forget to react with 👍 or 👎 to teach Ellipsis.
1. instructor/processing/response.py:254
  • Draft comment:
    When setting _raw_response on model.content for AdapterBase, please ensure that model.content is not None (or verify it has the expected attribute) to avoid potential runtime errors.
  • Reason this comment was not posted:
    Comment did not seem useful. Confidence is useful = 0% <= threshold 85% This comment is asking the author to ensure that a certain condition is met to avoid runtime errors. It is not making a specific suggestion or pointing out a specific issue with the code. It is more of a general cautionary note, which violates the rule against asking the author to ensure or verify things.
2. instructor/processing/response.py:357
  • Draft comment:
    In the synchronous branch, the same logic attaches _raw_response to model.content. Consider verifying that model.content is valid, and if possible refactor this duplicated logic into a helper function.
  • Reason this comment was not posted:
    Decided after close inspection that this draft comment was likely wrong and/or not actionable: usefulness confidence = 80% vs. threshold = 85% The comment is about code that was just changed in this PR - specifically the addition of model.content._raw_response = response in both functions. The duplication observation is accurate. The suggestion to refactor into a helper function is reasonable and actionable. The comment about verifying model.content is valid seems less important since this code path is specifically for AdapterBase instances which should guarantee content exists. The comment combines two separate suggestions - one about refactoring duplicated code (good) and one about validation (less relevant). Could the refactoring suggestion be made more specific? While the validation part is less useful, the core suggestion about reducing code duplication through refactoring is valuable and directly related to the changes. The duplicated logic is simple enough that the refactoring approach is fairly obvious. Keep the comment since it identifies real code duplication introduced by this change and suggests a reasonable refactoring solution. The validation suggestion is less important but doesn't detract significantly from the main point.

Workflow ID: wflow_m5CS3hmIvK2MtJXo

You can customize Ellipsis by changing your verbosity settings, reacting with 👍 or 👎, replying to comments, or adding code review rules.

@edge7
Copy link
Author

edge7 commented Jul 31, 2025

Can I help, in anything here?

@edge7
Copy link
Author

edge7 commented Aug 26, 2025

Hello!
I needed to patch the code manually in my Ci cd. Can we get this merged? Or am missing something here?

@sapountzis
Copy link
Contributor

@edge7 I think there is an issue with the code you provided

according to the docs the correct code should be

import instructor
from pydantic import BaseModel

MODEL = "openai/o3"
client = instructor.from_provider(MODEL)


class Name(BaseModel):
    name: str


(name, completion) = client.chat.completions.create_with_completion(
    model=MODEL.split("/")[-1],
    messages=[
        {"role": "system", "content": "extract info when possible"},
        {"content": "my name is Enrico", "role": "user"},
    ],
    response_model=Name,
    max_retries=3,
)
print(completion.usage)

@edge7
Copy link
Author

edge7 commented Sep 2, 2025

What is the difference? Does your code work without any change? Cheers

@sapountzis
Copy link
Contributor

sapountzis commented Sep 2, 2025

Hmm indeed looking at it again it's not different yet locally it works

CompletionUsage(completion_tokens=712, prompt_tokens=4466, total_tokens=5178, completion_tokens_details=CompletionTokensDetails(accepted_prediction_tokens=None, audio_tokens=0, reasoning_tokens=0, rejected_prediction_tokens=None), prompt_tokens_details=PromptTokensDetails(audio_tokens=0, cached_tokens=0))

But I am not using from_provider

Instead I use instructor.from_openai(AsyncOpenAI())

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants