Skip to content

Conversation

@fatelei
Copy link
Contributor

@fatelei fatelei commented Aug 17, 2025

fix #886

@DouweM DouweM changed the title fix: https://github.com/pydantic/pydantic-ai/issues/886 Add id and finish_reason to ModelResponse Aug 25, 2025
@DouweM
Copy link
Collaborator

DouweM commented Sep 1, 2025

@fatelei Please have a look as well at the remaining comments from the previous PR at #2325.

@DouweM DouweM self-assigned this Sep 1, 2025
@fatelei fatelei force-pushed the issue-886 branch 2 times, most recently from 4422cb0 to 35185ad Compare September 2, 2025 11:05
@fatelei
Copy link
Contributor Author

fatelei commented Sep 2, 2025

spell check in pre commit failed

if chunk.response.id and self.provider_response_id is None:
self.provider_response_id = chunk.response.id
if self.finish_reason is None:
incomplete_reason = getattr(getattr(chunk.response, 'incomplete_details', None), 'reason', None)
Copy link
Collaborator

Choose a reason for hiding this comment

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

As above, why do we need getattr here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Using nested getattr(..., default=None) safely handles both cases without exceptions and without assuming dict access:
• getattr(chunk.response, 'incomplete_details', None) guards missing field
• getattr(..., 'reason', None) guards missing reason on that object

An alternative like (incomplete_details or {}).get('reason') won’t work reliably because incomplete_details isn’t always a dict.

@fatelei fatelei force-pushed the issue-886 branch 2 times, most recently from e81aabc to 23f12ba Compare September 3, 2025 10:43
{
**response.usage.opentelemetry_attributes(),
'gen_ai.response.model': response_model,
events = self.instrumentation_settings.messages_to_otel_events(messages)
Copy link
Collaborator

Choose a reason for hiding this comment

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

These changes shouldn't be necessary (anymore)

@DouweM
Copy link
Collaborator

DouweM commented Sep 3, 2025

@fatelei Please merge main, there are some merge conflicts that are preventing the tests and linter to run in CI

@DouweM DouweM changed the title Add id and finish_reason to ModelResponse Add ModelResponse.finish_reason and set provider_response_id while streaming Sep 5, 2025
@DouweM DouweM changed the title Add ModelResponse.finish_reason and set provider_response_id while streaming Add ModelResponse.finish_reason, set provider_response_id while streaming, set both on OTel spans Sep 8, 2025
@DouweM DouweM changed the title Add ModelResponse.finish_reason, set provider_response_id while streaming, set both on OTel spans Add ModelResponse.finish_reason and set provider_response_id while streaming Sep 8, 2025
@DouweM DouweM enabled auto-merge (squash) September 8, 2025 23:56
@DouweM DouweM merged commit 0047a68 into pydantic:main Sep 9, 2025
55 of 58 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add id and finish_reason to ModelResponse

3 participants