diff --git a/docs/docs/modules/model_io/chat/structured_output.ipynb b/docs/docs/modules/model_io/chat/structured_output.ipynb index aa0c956ee10..e76f9926438 100644 --- a/docs/docs/modules/model_io/chat/structured_output.ipynb +++ b/docs/docs/modules/model_io/chat/structured_output.ipynb @@ -15,7 +15,7 @@ "id": "6e3f0f72", "metadata": {}, "source": [ - "# [beta] Structured Output\n", + "# Structured Output\n", "\n", "It is often crucial to have LLMs return structured output. This is because oftentimes the outputs of the LLMs are used in downstream applications, where specific arguments are required. Having the LLM return structured output reliably is necessary for that.\n", "\n", @@ -39,21 +39,14 @@ }, { "cell_type": "code", - "execution_count": 2, - "id": "08029f4e", - "metadata": {}, - "outputs": [], - "source": [ - "from langchain_core.pydantic_v1 import BaseModel, Field" - ] - }, - { - "cell_type": "code", - "execution_count": 3, + "execution_count": 1, "id": "070bf702", "metadata": {}, "outputs": [], "source": [ + "from langchain_core.pydantic_v1 import BaseModel, Field\n", + "\n", + "\n", "class Joke(BaseModel):\n", " setup: str = Field(description=\"The setup of the joke\")\n", " punchline: str = Field(description=\"The punchline to the joke\")" @@ -93,7 +86,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 3, "id": "6700994a", "metadata": {}, "outputs": [], @@ -104,17 +97,17 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 4, "id": "c55a61b8", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "Joke(setup='Why was the cat sitting on the computer?', punchline='It wanted to keep an eye on the mouse!')" + "Joke(setup='Why was the cat sitting on the computer?', punchline='To keep an eye on the mouse!')" ] }, - "execution_count": 10, + "execution_count": 4, "metadata": {}, "output_type": "execute_result" } @@ -135,7 +128,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 5, "id": "df0370e3", "metadata": {}, "outputs": [], @@ -145,17 +138,17 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 6, "id": "23844a26", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "Joke(setup=\"Why don't cats play poker in the jungle?\", punchline='Too many cheetahs!')" + "Joke(setup='Why was the cat sitting on the computer?', punchline='Because it wanted to keep an eye on the mouse!')" ] }, - "execution_count": 14, + "execution_count": 6, "metadata": {}, "output_type": "execute_result" } @@ -180,7 +173,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 7, "id": "ad45fdd8", "metadata": {}, "outputs": [], @@ -252,7 +245,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 11, "id": "649f9632", "metadata": {}, "outputs": [ @@ -262,7 +255,7 @@ "Joke(setup='Why did the dog sit in the shade?', punchline='To avoid getting burned.')" ] }, - "execution_count": 12, + "execution_count": 11, "metadata": {}, "output_type": "execute_result" } @@ -287,7 +280,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 12, "id": "bffd3fad", "metadata": {}, "outputs": [], @@ -297,7 +290,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 13, "id": "c8bd7549", "metadata": {}, "outputs": [], @@ -308,10 +301,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 14, "id": "17b15816", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "Joke(setup=\"Why don't cats play poker in the jungle?\", punchline='Too many cheetahs!')" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "structured_llm.invoke(\"Tell me a joke about cats\")" ] @@ -328,7 +332,7 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": 15, "id": "9b9617e3", "metadata": {}, "outputs": [], @@ -340,7 +344,7 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": 16, "id": "90549664", "metadata": {}, "outputs": [], @@ -355,7 +359,7 @@ }, { "cell_type": "code", - "execution_count": 25, + "execution_count": 17, "id": "01da39be", "metadata": {}, "outputs": [ @@ -365,7 +369,7 @@ "Joke(setup='Why did the cat sit on the computer?', punchline='To keep an eye on the mouse!')" ] }, - "execution_count": 25, + "execution_count": 17, "metadata": {}, "output_type": "execute_result" } @@ -388,7 +392,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 18, "id": "70511bc3", "metadata": {}, "outputs": [], @@ -408,19 +412,10 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 19, "id": "be9fdf04", "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "/Users/reag/src/langchain/libs/core/langchain_core/_api/beta_decorator.py:87: LangChainBetaWarning: The function `with_structured_output` is in beta. It is actively being worked on, so the API may change.\n", - " warn_beta(\n" - ] - } - ], + "outputs": [], "source": [ "model = ChatGroq()\n", "structured_llm = model.with_structured_output(Joke)" @@ -428,7 +423,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 20, "id": "e13f4676", "metadata": {}, "outputs": [ @@ -438,7 +433,7 @@ "Joke(setup=\"Why don't cats play poker in the jungle?\", punchline='Too many cheetahs!')" ] }, - "execution_count": 7, + "execution_count": 20, "metadata": {}, "output_type": "execute_result" } @@ -459,7 +454,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 21, "id": "86574fb8", "metadata": {}, "outputs": [], @@ -469,7 +464,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 22, "id": "01dced9c", "metadata": {}, "outputs": [ @@ -479,7 +474,7 @@ "Joke(setup=\"Why don't cats play poker in the jungle?\", punchline='Too many cheetahs!')" ] }, - "execution_count": 9, + "execution_count": 22, "metadata": {}, "output_type": "execute_result" } @@ -504,7 +499,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 23, "id": "12682237-6689-4408-88b1-3595feac447f", "metadata": {}, "outputs": [ @@ -514,7 +509,7 @@ "Joke(setup='What do you call a cat that loves to bowl?', punchline='An alley cat!')" ] }, - "execution_count": 5, + "execution_count": 23, "metadata": {}, "output_type": "execute_result" } @@ -541,17 +536,17 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 2, "id": "24421189-02bf-4589-a91a-197584c4a696", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "Joke(setup='A cat-ch', punchline='What do you call a cat that loves to play fetch?')" + "Joke(setup='Why did the scarecrow win an award?', punchline='Why did the scarecrow win an award? Because he was outstanding in his field.')" ] }, - "execution_count": 7, + "execution_count": 2, "metadata": {}, "output_type": "execute_result" } @@ -563,13 +558,21 @@ "structured_llm = llm.with_structured_output(Joke)\n", "structured_llm.invoke(\"Tell me a joke about cats\")" ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "2630a2cb", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "poetry-venv-2", + "display_name": ".venv", "language": "python", - "name": "poetry-venv-2" + "name": "python3" }, "language_info": { "codemirror_mode": { @@ -581,7 +584,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.1" + "version": "3.11.4" } }, "nbformat": 4, diff --git a/libs/core/langchain_core/language_models/base.py b/libs/core/langchain_core/language_models/base.py index a6addf8f6b0..a62809fbe9c 100644 --- a/libs/core/langchain_core/language_models/base.py +++ b/libs/core/langchain_core/language_models/base.py @@ -19,7 +19,7 @@ from typing import ( from typing_extensions import TypeAlias -from langchain_core._api import beta, deprecated +from langchain_core._api import deprecated from langchain_core.messages import ( AnyMessage, BaseMessage, @@ -201,7 +201,6 @@ class BaseLanguageModel( prompt and additional model provider-specific output. """ - @beta() def with_structured_output( self, schema: Union[Dict, Type[BaseModel]], **kwargs: Any ) -> Runnable[LanguageModelInput, Union[Dict, BaseModel]]: diff --git a/libs/partners/anthropic/langchain_anthropic/chat_models.py b/libs/partners/anthropic/langchain_anthropic/chat_models.py index 5b2c715029a..29a75ba9bdd 100644 --- a/libs/partners/anthropic/langchain_anthropic/chat_models.py +++ b/libs/partners/anthropic/langchain_anthropic/chat_models.py @@ -580,7 +580,6 @@ class ChatAnthropic(BaseChatModel): formatted_tools = [convert_to_anthropic_tool(tool) for tool in tools] return self.bind(tools=formatted_tools, **kwargs) - @beta() def with_structured_output( self, schema: Union[Dict, Type[BaseModel]], diff --git a/libs/partners/fireworks/langchain_fireworks/chat_models.py b/libs/partners/fireworks/langchain_fireworks/chat_models.py index fc5960eea98..52dea080894 100644 --- a/libs/partners/fireworks/langchain_fireworks/chat_models.py +++ b/libs/partners/fireworks/langchain_fireworks/chat_models.py @@ -24,7 +24,6 @@ from typing import ( ) from fireworks.client import AsyncFireworks, Fireworks # type: ignore -from langchain_core._api import beta from langchain_core.callbacks import ( AsyncCallbackManagerForLLMRun, CallbackManagerForLLMRun, @@ -671,7 +670,6 @@ class ChatFireworks(BaseChatModel): kwargs["tool_choice"] = tool_choice return super().bind(tools=formatted_tools, **kwargs) - @beta() def with_structured_output( self, schema: Optional[Union[Dict, Type[BaseModel]]] = None, diff --git a/libs/partners/groq/langchain_groq/chat_models.py b/libs/partners/groq/langchain_groq/chat_models.py index 2650f7608c2..86db80e8e22 100644 --- a/libs/partners/groq/langchain_groq/chat_models.py +++ b/libs/partners/groq/langchain_groq/chat_models.py @@ -23,7 +23,6 @@ from typing import ( cast, ) -from langchain_core._api import beta from langchain_core.callbacks import ( AsyncCallbackManagerForLLMRun, CallbackManagerForLLMRun, @@ -595,7 +594,6 @@ class ChatGroq(BaseChatModel): kwargs["tool_choice"] = tool_choice return super().bind(tools=formatted_tools, **kwargs) - @beta() def with_structured_output( self, schema: Optional[Union[Dict, Type[BaseModel]]] = None, diff --git a/libs/partners/mistralai/langchain_mistralai/chat_models.py b/libs/partners/mistralai/langchain_mistralai/chat_models.py index ab3027c94d9..a00943d2c00 100644 --- a/libs/partners/mistralai/langchain_mistralai/chat_models.py +++ b/libs/partners/mistralai/langchain_mistralai/chat_models.py @@ -22,7 +22,6 @@ from typing import ( import httpx from httpx_sse import EventSource, aconnect_sse, connect_sse -from langchain_core._api import beta from langchain_core.callbacks import ( AsyncCallbackManagerForLLMRun, CallbackManagerForLLMRun, @@ -588,7 +587,6 @@ class ChatMistralAI(BaseChatModel): formatted_tools = [convert_to_openai_tool(tool) for tool in tools] return super().bind(tools=formatted_tools, **kwargs) - @beta() def with_structured_output( self, schema: Union[Dict, Type[BaseModel]], diff --git a/libs/partners/openai/langchain_openai/chat_models/base.py b/libs/partners/openai/langchain_openai/chat_models/base.py index 915557baa56..e68377c2a85 100644 --- a/libs/partners/openai/langchain_openai/chat_models/base.py +++ b/libs/partners/openai/langchain_openai/chat_models/base.py @@ -29,7 +29,6 @@ from typing import ( import openai import tiktoken -from langchain_core._api import beta from langchain_core.callbacks import ( AsyncCallbackManagerForLLMRun, CallbackManagerForLLMRun, @@ -885,7 +884,6 @@ class ChatOpenAI(BaseChatModel): ) -> Runnable[LanguageModelInput, _DictOrPydantic]: ... - @beta() def with_structured_output( self, schema: Optional[_DictOrPydanticClass] = None,