Skip to content

Commit f7e2c01

Browse files
committed
Add support for structured outputs
1 parent 95ec11f commit f7e2c01

File tree

4 files changed

+10
-1
lines changed

4 files changed

+10
-1
lines changed

coagent/agents/aswarm/core.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ async def get_chat_completion(
4444
model_override: str,
4545
stream: bool,
4646
debug: bool,
47+
response_format: dict | None = None,
4748
) -> ChatCompletionMessage:
4849
context_variables = defaultdict(str, context_variables)
4950
instructions = (
@@ -65,6 +66,7 @@ async def get_chat_completion(
6566
create_params = {
6667
"model": model_override or agent.model,
6768
"messages": messages,
69+
"response_format": response_format,
6870
"tools": tools or None,
6971
"tool_choice": agent.tool_choice,
7072
"stream": stream,
@@ -217,6 +219,7 @@ async def run_and_stream(
217219
self,
218220
agent: Agent,
219221
messages: List,
222+
response_format: dict | None = None,
220223
context_variables: dict = {},
221224
model_override: str = None,
222225
debug: bool = False,
@@ -248,6 +251,7 @@ async def run_and_stream(
248251
completion = self.get_chat_completion(
249252
agent=active_agent,
250253
history=history,
254+
response_format=response_format,
251255
context_variables=context_variables,
252256
model_override=model_override,
253257
stream=True,

coagent/agents/chat_agent.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ async def handle_history(
241241
async def handle_message(
242242
self, msg: ChatMessage, ctx: Context
243243
) -> AsyncIterator[ChatMessage]:
244-
history = ChatHistory(messages=[msg])
244+
history = ChatHistory(messages=[msg], response_format=msg.response_format)
245245
response = self._handle_history(history, ctx)
246246
async for resp in response:
247247
yield resp
@@ -260,6 +260,7 @@ async def _handle_history(
260260
response = self._swarm_client.run_and_stream(
261261
agent=swarm_agent,
262262
messages=[m.model_dump() for m in msg.messages],
263+
response_format=msg.response_format,
263264
context_variables=msg.extensions,
264265
)
265266
async for resp in response:

coagent/agents/messages.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
class ChatMessage(Message):
1010
role: str
1111
content: str
12+
response_format: dict | None = None
1213

1314
type: str = Field(default="", description="The type of the message. e.g. confirm")
1415
sender: str = Field(default="", description="The sending agent of the message.")
@@ -29,3 +30,4 @@ def to_llm_message(self) -> dict[str, Any]:
2930

3031
class ChatHistory(Message):
3132
messages: list[ChatMessage]
33+
response_format: dict | None = None

coagent/agents/model_client.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ async def acompletion(
3939
temperature: float = 0.1,
4040
tools: list | None = None,
4141
tool_choice: str | None = None,
42+
response_format: dict | None = None,
4243
**kwargs,
4344
): # -> ModelResponse:
4445
import litellm
@@ -54,6 +55,7 @@ async def acompletion(
5455
api_base=self.api_base,
5556
api_version=self.api_version,
5657
api_key=self.api_key,
58+
response_format=response_format,
5759
**kwargs,
5860
)
5961
return response

0 commit comments

Comments
 (0)