Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 52 additions & 6 deletions src/google/adk/integrations/openai/_openai_llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,12 @@ def _content_to_openai_messages(
else ""
),
})
elif part.thought and role == "assistant":
# Don't replay the model's earlier reasoning, matching
# OpenAIResponsesLlm. Sent back as "Thought: ..." text it would read as
# part of the visible answer, and DeepSeek asks clients not to send
# reasoning_content back in later requests.
continue
else:
content_parts.append(_part_to_openai_content(part))

Expand Down Expand Up @@ -282,6 +288,26 @@ def _usage_metadata(
)


# OpenAI-compatible servers return reasoning text in a field the OpenAI SDK
# keeps as an untyped extra attribute: vLLM main uses ``reasoning``, while
# SGLang, the DeepSeek API and older vLLM releases use ``reasoning_content``.
_REASONING_FIELDS = ("reasoning_content", "reasoning")


def _extract_reasoning_text(message_or_delta: Any) -> str | None:
"""Returns the reasoning text on a message or stream delta, if any."""
for field in _REASONING_FIELDS:
value = getattr(message_or_delta, field, None)
if isinstance(value, str) and value:
return value
return None


def _has_visible_output(parts: list[types.Part]) -> bool:
"""Returns True if any part is something other than a thought."""
return any(not part.thought for part in parts)


def _tool_call_parts(message: ChatCompletionMessage) -> list[types.Part]:
"""Converts OpenAI tool calls on a message to ADK function-call parts."""
parts: list[types.Part] = []
Expand Down Expand Up @@ -321,14 +347,20 @@ def _response_to_llm_response(response: ChatCompletion) -> LlmResponse:
message = choice.message

parts = []
reasoning_text = _extract_reasoning_text(message)
if reasoning_text:
parts.append(types.Part(text=reasoning_text, thought=True))
if message.content:
parts.append(types.Part.from_text(text=message.content))
parts.extend(_tool_call_parts(message))

raw_finish_reason = getattr(choice, "finish_reason", None)
finish_reason = _map_finish_reason(raw_finish_reason)

if not parts and finish_reason not in (None, types.FinishReason.STOP):
if not _has_visible_output(parts) and finish_reason not in (
None,
types.FinishReason.STOP,
):
# No usable content and the model stopped for an abnormal reason (e.g.
# content filtering or hitting the token limit before emitting anything).
# Mirror LlmResponse.create and surface it as an error. A truncated-but-
Expand Down Expand Up @@ -531,6 +563,7 @@ async def _generate_content_streaming(
raw_stream = await self._openai_client.chat.completions.create(**kwargs)

text_accumulated = ""
reasoning_accumulated = ""
tool_calls_accumulated: dict[int, dict[str, Any]] = {}
usage: CompletionUsage | None = None
finish_reason: str | None = None
Expand All @@ -545,6 +578,17 @@ async def _generate_content_streaming(
finish_reason = choice.finish_reason
delta = choice.delta

reasoning_delta = _extract_reasoning_text(delta)
if reasoning_delta:
reasoning_accumulated += reasoning_delta
yield LlmResponse(
content=types.Content(
role="model",
parts=[types.Part(text=reasoning_delta, thought=True)],
),
partial=True,
)

if delta.content:
text_accumulated += delta.content
yield LlmResponse(
Expand Down Expand Up @@ -601,6 +645,8 @@ async def _generate_content_streaming(

# Yield final response with all accumulated content
parts = []
if reasoning_accumulated:
parts.append(types.Part(text=reasoning_accumulated, thought=True))
if text_accumulated:
parts.append(types.Part.from_text(text=text_accumulated))

Expand All @@ -625,14 +671,14 @@ async def _generate_content_streaming(

mapped_finish_reason = _map_finish_reason(finish_reason)

if not parts and mapped_finish_reason not in (
if not _has_visible_output(parts) and mapped_finish_reason not in (
None,
types.FinishReason.STOP,
):
# Nothing was streamed and the model stopped for an abnormal reason (e.g.
# content filtering, or hitting the token limit before emitting anything).
# Mirror the non-streaming path and surface it as an error rather than a
# silent empty final chunk.
# Nothing but reasoning (if anything) was streamed and the model stopped
# for an abnormal reason (e.g. content filtering, or hitting the token
# limit before emitting an answer). Mirror the non-streaming path and
# surface it as an error rather than a silent empty final chunk.
yield LlmResponse(
error_code=mapped_finish_reason,
error_message=(
Expand Down
Loading