1
0
mirror of https://github.com/hwchase17/langchain.git synced 2025-05-09 17:18:31 +00:00
langchain/libs/partners
Dmitrii Rashchenko a43df006de
Support of openai reasoning summary streaming ()
**langchain_openai: Support of reasoning summary streaming**

**Description:**
OpenAI API now supports streaming reasoning summaries for reasoning
models (o1, o3, o3-mini, o4-mini). More info about it:
https://platform.openai.com/docs/guides/reasoning#reasoning-summaries

It is supported only in Responses API (not Completion API), so you need
to create LangChain Open AI model as follows to support reasoning
summaries streaming:

```
llm = ChatOpenAI(
    model="o4-mini", # also o1, o3, o3-mini support reasoning streaming
    use_responses_api=True,  # reasoning streaming works only with responses api, not completion api
    model_kwargs={
        "reasoning": {
            "effort": "high",  # also "low" and "medium" supported
            "summary": "auto"  # some models support "concise" summary, some "detailed", but auto will always work
        }
    }
)
```

Now, if you stream events from llm:

```
async for event in llm.astream_events(prompt, version="v2"):
    print(event)
```

or

```
for chunk in llm.stream(prompt):
    print (chunk)
```

OpenAI API will send you new types of events:
`response.reasoning_summary_text.added`
`response.reasoning_summary_text.delta`
`response.reasoning_summary_text.done`

These events are new, so they were ignored. So I have added support of
these events in function `_convert_responses_chunk_to_generation_chunk`,
so reasoning chunks or full reasoning added to the chunk
additional_kwargs.

Example of how this reasoning summary may be printed:

```
    async for event in llm.astream_events(prompt, version="v2"):
        if event["event"] == "on_chat_model_stream":
            chunk: AIMessageChunk = event["data"]["chunk"]
            if "reasoning_summary_chunk" in chunk.additional_kwargs:
                print(chunk.additional_kwargs["reasoning_summary_chunk"], end="")
            elif "reasoning_summary" in chunk.additional_kwargs:
                print("\n\nFull reasoning step summary:", chunk.additional_kwargs["reasoning_summary"])
            elif chunk.content and chunk.content[0]["type"] == "text":
                print(chunk.content[0]["text"], end="")
```

or

```
    for chunk in llm.stream(prompt):
        if "reasoning_summary_chunk" in chunk.additional_kwargs:
            print(chunk.additional_kwargs["reasoning_summary_chunk"], end="")
        elif "reasoning_summary" in chunk.additional_kwargs:
            print("\n\nFull reasoning step summary:", chunk.additional_kwargs["reasoning_summary"])
        elif chunk.content and chunk.content[0]["type"] == "text":
            print(chunk.content[0]["text"], end="")
```

---------

Co-authored-by: Chester Curme <chester.curme@gmail.com>
2025-04-22 14:51:13 +00:00
..
ai21 ai21: migrate to external repo () 2024-08-28 14:24:07 -07:00
anthropic anthropic[patch]: make description optional on AnthropicTool () 2025-04-21 10:44:39 -04:00
astradb Add README for astradb package () 2024-08-13 09:17:23 -04:00
azure-dynamic-sessions azure-dynamic-sessions: migrate to repo () 2024-10-18 12:30:48 -07:00
chroma chroma: release 0.2.3 () 2025-04-15 14:11:23 -04:00
couchbase couchbase: Migrate couchbase partner package to different repo () 2025-01-15 12:37:27 -08:00
deepseek partners[lint]: run pyupgrade to get code in line with 3.9 standards () 2025-04-11 07:18:44 -04:00
exa partners[lint]: run pyupgrade to get code in line with 3.9 standards () 2025-04-11 07:18:44 -04:00
fireworks fireworks[patch]: update model in LLM integration tests () 2025-04-21 17:53:27 +00:00
groq partners[lint]: run pyupgrade to get code in line with 3.9 standards () 2025-04-11 07:18:44 -04:00
huggingface partners: bug fix check_imports.py exit code. () 2025-04-17 08:02:23 -04:00
ibm ibm: move to external repo () 2024-07-12 21:14:24 +00:00
milvus milvus: mv to external repo () 2024-10-01 00:38:30 +00:00
mistralai partners[lint]: run pyupgrade to get code in line with 3.9 standards () 2025-04-11 07:18:44 -04:00
mongodb mongodb[patch]: fix link in readme () 2025-02-11 18:19:59 +00:00
nomic partners[lint]: run pyupgrade to get code in line with 3.9 standards () 2025-04-11 07:18:44 -04:00
ollama langchain-ollama (partners) / langchain-core: allow passing ChatMessages to Ollama (including arbitrary roles) () 2025-04-18 10:07:07 -04:00
openai Support of openai reasoning summary streaming () 2025-04-22 14:51:13 +00:00
perplexity perplexity: release 0.1.1 () 2025-04-15 18:02:15 +00:00
pinecone pinecone: delete from monorepo () 2025-02-19 12:55:15 -05:00
prompty partners: bug fix check_imports.py exit code. () 2025-04-17 08:02:23 -04:00
qdrant partners[lint]: run pyupgrade to get code in line with 3.9 standards () 2025-04-11 07:18:44 -04:00
together together: mv to external repo () 2024-08-29 16:42:59 -07:00
unstructured unstructured: mv to external repo () 2024-09-30 17:38:21 -07:00
voyageai partners[lint]: run pyupgrade to get code in line with 3.9 standards () 2025-04-11 07:18:44 -04:00
xai xai: release 0.2.3 () 2025-04-11 14:05:11 +00:00