dialectus-engine 0.2.2__tar.gz → 0.3.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (72) hide show
  1. {dialectus_engine-0.2.2/dialectus_engine.egg-info → dialectus_engine-0.3.0}/PKG-INFO +1 -1
  2. {dialectus_engine-0.2.2 → dialectus_engine-0.3.0}/dialectus/__init__.py +1 -1
  3. {dialectus_engine-0.2.2 → dialectus_engine-0.3.0}/dialectus/engine/debate_engine/engine.py +11 -8
  4. {dialectus_engine-0.2.2 → dialectus_engine-0.3.0}/dialectus/engine/debate_engine/prompt_builder.py +34 -29
  5. {dialectus_engine-0.2.2 → dialectus_engine-0.3.0}/dialectus/engine/debate_engine/response_handler.py +2 -3
  6. {dialectus_engine-0.2.2 → dialectus_engine-0.3.0}/dialectus/engine/debate_engine/round_manager.py +12 -9
  7. dialectus_engine-0.3.0/dialectus/engine/debate_engine/types.py +97 -0
  8. {dialectus_engine-0.2.2 → dialectus_engine-0.3.0}/dialectus/engine/judges/ai_judge.py +65 -58
  9. {dialectus_engine-0.2.2 → dialectus_engine-0.3.0}/dialectus/engine/models/manager.py +2 -2
  10. {dialectus_engine-0.2.2 → dialectus_engine-0.3.0}/dialectus/engine/models/providers/base_model_provider.py +3 -3
  11. {dialectus_engine-0.2.2 → dialectus_engine-0.3.0}/dialectus/engine/models/providers/ollama_provider.py +2 -2
  12. {dialectus_engine-0.2.2 → dialectus_engine-0.3.0}/dialectus/engine/models/providers/open_router_provider.py +2 -2
  13. {dialectus_engine-0.2.2 → dialectus_engine-0.3.0}/dialectus/engine/moderation/llm_moderator.py +15 -12
  14. {dialectus_engine-0.2.2 → dialectus_engine-0.3.0}/dialectus/engine/moderation/openai_moderator.py +41 -4
  15. {dialectus_engine-0.2.2 → dialectus_engine-0.3.0/dialectus_engine.egg-info}/PKG-INFO +1 -1
  16. {dialectus_engine-0.2.2 → dialectus_engine-0.3.0}/pyproject.toml +1 -1
  17. {dialectus_engine-0.2.2 → dialectus_engine-0.3.0}/tests/test_moderation_openai.py +67 -6
  18. dialectus_engine-0.2.2/dialectus/engine/debate_engine/types.py +0 -23
  19. {dialectus_engine-0.2.2 → dialectus_engine-0.3.0}/LICENSE +0 -0
  20. {dialectus_engine-0.2.2 → dialectus_engine-0.3.0}/README.md +0 -0
  21. {dialectus_engine-0.2.2 → dialectus_engine-0.3.0}/dialectus/engine/__init__.py +0 -0
  22. {dialectus_engine-0.2.2 → dialectus_engine-0.3.0}/dialectus/engine/config/__init__.py +0 -0
  23. {dialectus_engine-0.2.2 → dialectus_engine-0.3.0}/dialectus/engine/config/openrouter_filters.json +0 -0
  24. {dialectus_engine-0.2.2 → dialectus_engine-0.3.0}/dialectus/engine/config/settings.py +0 -0
  25. {dialectus_engine-0.2.2 → dialectus_engine-0.3.0}/dialectus/engine/debate_engine/__init__.py +0 -0
  26. {dialectus_engine-0.2.2 → dialectus_engine-0.3.0}/dialectus/engine/debate_engine/context_builder.py +0 -0
  27. {dialectus_engine-0.2.2 → dialectus_engine-0.3.0}/dialectus/engine/debate_engine/judge_coordinator.py +0 -0
  28. {dialectus_engine-0.2.2 → dialectus_engine-0.3.0}/dialectus/engine/debate_engine/models.py +0 -0
  29. {dialectus_engine-0.2.2 → dialectus_engine-0.3.0}/dialectus/engine/debate_engine/utils.py +0 -0
  30. {dialectus_engine-0.2.2 → dialectus_engine-0.3.0}/dialectus/engine/formats/__init__.py +0 -0
  31. {dialectus_engine-0.2.2 → dialectus_engine-0.3.0}/dialectus/engine/formats/base.py +0 -0
  32. {dialectus_engine-0.2.2 → dialectus_engine-0.3.0}/dialectus/engine/formats/oxford.py +0 -0
  33. {dialectus_engine-0.2.2 → dialectus_engine-0.3.0}/dialectus/engine/formats/parliamentary.py +0 -0
  34. {dialectus_engine-0.2.2 → dialectus_engine-0.3.0}/dialectus/engine/formats/public_forum.py +0 -0
  35. {dialectus_engine-0.2.2 → dialectus_engine-0.3.0}/dialectus/engine/formats/registry.py +0 -0
  36. {dialectus_engine-0.2.2 → dialectus_engine-0.3.0}/dialectus/engine/formats/socratic.py +0 -0
  37. {dialectus_engine-0.2.2 → dialectus_engine-0.3.0}/dialectus/engine/formats/topic_themes.py +0 -0
  38. {dialectus_engine-0.2.2 → dialectus_engine-0.3.0}/dialectus/engine/judges/__init__.py +0 -0
  39. {dialectus_engine-0.2.2 → dialectus_engine-0.3.0}/dialectus/engine/judges/base.py +0 -0
  40. {dialectus_engine-0.2.2 → dialectus_engine-0.3.0}/dialectus/engine/judges/ensemble_utils.py +0 -0
  41. {dialectus_engine-0.2.2 → dialectus_engine-0.3.0}/dialectus/engine/judges/factory.py +0 -0
  42. {dialectus_engine-0.2.2 → dialectus_engine-0.3.0}/dialectus/engine/models/__init__.py +0 -0
  43. {dialectus_engine-0.2.2 → dialectus_engine-0.3.0}/dialectus/engine/models/base_types.py +0 -0
  44. {dialectus_engine-0.2.2 → dialectus_engine-0.3.0}/dialectus/engine/models/cache_manager.py +0 -0
  45. {dialectus_engine-0.2.2 → dialectus_engine-0.3.0}/dialectus/engine/models/openrouter/openrouter_architecture.py +0 -0
  46. {dialectus_engine-0.2.2 → dialectus_engine-0.3.0}/dialectus/engine/models/openrouter/openrouter_capability_extractor.py +0 -0
  47. {dialectus_engine-0.2.2 → dialectus_engine-0.3.0}/dialectus/engine/models/openrouter/openrouter_enhanced_model_info.py +0 -0
  48. {dialectus_engine-0.2.2 → dialectus_engine-0.3.0}/dialectus/engine/models/openrouter/openrouter_filter_config.py +0 -0
  49. {dialectus_engine-0.2.2 → dialectus_engine-0.3.0}/dialectus/engine/models/openrouter/openrouter_model.py +0 -0
  50. {dialectus_engine-0.2.2 → dialectus_engine-0.3.0}/dialectus/engine/models/openrouter/openrouter_model_filter.py +0 -0
  51. {dialectus_engine-0.2.2 → dialectus_engine-0.3.0}/dialectus/engine/models/openrouter/openrouter_models_response.py +0 -0
  52. {dialectus_engine-0.2.2 → dialectus_engine-0.3.0}/dialectus/engine/models/openrouter/openrouter_pricing.py +0 -0
  53. {dialectus_engine-0.2.2 → dialectus_engine-0.3.0}/dialectus/engine/models/openrouter/openrouter_top_provider.py +0 -0
  54. {dialectus_engine-0.2.2 → dialectus_engine-0.3.0}/dialectus/engine/models/providers/__init__.py +0 -0
  55. {dialectus_engine-0.2.2 → dialectus_engine-0.3.0}/dialectus/engine/models/providers/exceptions.py +0 -0
  56. {dialectus_engine-0.2.2 → dialectus_engine-0.3.0}/dialectus/engine/models/providers/openrouter_generation_types.py +0 -0
  57. {dialectus_engine-0.2.2 → dialectus_engine-0.3.0}/dialectus/engine/models/providers/providers.py +0 -0
  58. {dialectus_engine-0.2.2 → dialectus_engine-0.3.0}/dialectus/engine/moderation/__init__.py +0 -0
  59. {dialectus_engine-0.2.2 → dialectus_engine-0.3.0}/dialectus/engine/moderation/base_moderator.py +0 -0
  60. {dialectus_engine-0.2.2 → dialectus_engine-0.3.0}/dialectus/engine/moderation/exceptions.py +0 -0
  61. {dialectus_engine-0.2.2 → dialectus_engine-0.3.0}/dialectus/engine/moderation/manager.py +0 -0
  62. {dialectus_engine-0.2.2 → dialectus_engine-0.3.0}/dialectus_engine.egg-info/SOURCES.txt +0 -0
  63. {dialectus_engine-0.2.2 → dialectus_engine-0.3.0}/dialectus_engine.egg-info/dependency_links.txt +0 -0
  64. {dialectus_engine-0.2.2 → dialectus_engine-0.3.0}/dialectus_engine.egg-info/requires.txt +0 -0
  65. {dialectus_engine-0.2.2 → dialectus_engine-0.3.0}/dialectus_engine.egg-info/top_level.txt +0 -0
  66. {dialectus_engine-0.2.2 → dialectus_engine-0.3.0}/setup.cfg +0 -0
  67. {dialectus_engine-0.2.2 → dialectus_engine-0.3.0}/tests/test_cache_manager.py +0 -0
  68. {dialectus_engine-0.2.2 → dialectus_engine-0.3.0}/tests/test_config_settings.py +0 -0
  69. {dialectus_engine-0.2.2 → dialectus_engine-0.3.0}/tests/test_debate_models.py +0 -0
  70. {dialectus_engine-0.2.2 → dialectus_engine-0.3.0}/tests/test_format_registry.py +0 -0
  71. {dialectus_engine-0.2.2 → dialectus_engine-0.3.0}/tests/test_formats_base.py +0 -0
  72. {dialectus_engine-0.2.2 → dialectus_engine-0.3.0}/tests/test_judges_factory.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: dialectus-engine
3
- Version: 0.2.2
3
+ Version: 0.3.0
4
4
  Summary: Dialectus AI debate engine framework
5
5
  Author: Dialectus AI
6
6
  License-Expression: MIT
@@ -4,4 +4,4 @@ from pkgutil import extend_path
4
4
 
5
5
  __path__ = extend_path(__path__, __name__)
6
6
  __all__: list[str] = []
7
- __version__ = "0.2.2"
7
+ __version__ = "0.3.0"
@@ -3,7 +3,6 @@
3
3
  import asyncio
4
4
  import logging
5
5
  import time
6
- from collections.abc import Awaitable, Callable
7
6
  from typing import Any
8
7
 
9
8
  from dialectus.engine.config.settings import AppConfig
@@ -17,7 +16,13 @@ from .models import DebateContext
17
16
  from .prompt_builder import PromptBuilder
18
17
  from .response_handler import ResponseHandler
19
18
  from .round_manager import RoundManager
20
- from .types import DebatePhase
19
+ from .types import (
20
+ ChunkCallback,
21
+ DebatePhase,
22
+ MessageEventCallback,
23
+ PhaseEventCallback,
24
+ PhaseEventType,
25
+ )
21
26
 
22
27
  logger = logging.getLogger(__name__)
23
28
 
@@ -102,11 +107,9 @@ class DebateEngine:
102
107
 
103
108
  async def run_full_debate(
104
109
  self,
105
- phase_callback: Callable[[str, dict[str, Any]], Awaitable[None]] | None = None,
106
- message_callback: (
107
- Callable[[str, dict[str, Any]], Awaitable[None]] | None
108
- ) = None,
109
- chunk_callback: Callable[[str, bool], Awaitable[None]] | None = None,
110
+ phase_callback: PhaseEventCallback | None = None,
111
+ message_callback: MessageEventCallback | None = None,
112
+ chunk_callback: ChunkCallback | None = None,
110
113
  ) -> DebateContext:
111
114
  """Run a complete debate from start to finish.
112
115
 
@@ -167,7 +170,7 @@ class DebateEngine:
167
170
  # Invoke phase callback if provided
168
171
  if phase_callback:
169
172
  await phase_callback(
170
- "phase_started",
173
+ PhaseEventType.PHASE_STARTED,
171
174
  {
172
175
  "phase": format_phase.name,
173
176
  "instruction": format_phase.instruction,
@@ -3,6 +3,7 @@
3
3
  from __future__ import annotations
4
4
 
5
5
  import logging
6
+ from textwrap import dedent
6
7
  from typing import TYPE_CHECKING
7
8
 
8
9
  from dialectus.engine.config.settings import AppConfig
@@ -78,31 +79,33 @@ class PromptBuilder:
78
79
  """
79
80
  format_instructions = self.format.get_format_instructions()
80
81
 
81
- return f"""You are participating in a formal debate about: "{context.topic}"
82
+ return dedent(f"""
83
+ You are participating in a formal debate about: "{context.topic}"
82
84
 
83
- DEBATE FORMAT: {self.format.name.upper()}
84
- WORD LIMIT: {self.config.debate.word_limit} words or less per response
85
+ DEBATE FORMAT: {self.format.name.upper()}
86
+ WORD LIMIT: {self.config.debate.word_limit} words or less per response
85
87
 
86
- {format_instructions}
88
+ {format_instructions}
87
89
 
88
- GENERAL RULES:
89
- 1. Stay focused on the topic
90
- 2. Keep your response under {self.config.debate.word_limit} words
91
- 3. Provide evidence and reasoning
92
- 4. Address opponent's arguments
93
- 5. Maintain a respectful tone
94
- 6. Follow the debate format structure
90
+ GENERAL RULES:
91
+ 1. Stay focused on the topic
92
+ 2. Keep your response under {self.config.debate.word_limit} words
93
+ 3. Provide evidence and reasoning
94
+ 4. Address opponent's arguments
95
+ 5. Maintain a respectful tone
96
+ 6. Follow the debate format structure
95
97
 
96
- RESPONSE FORMAT:
97
- - Speak directly as your assigned role without any labels, prefixes, or announcements
98
- - Use plain text without markdown formatting (avoid **bold**, *italics*, # headers,
99
- bullet points)
100
- - Write in natural conversational style as if speaking live to an audience
101
- - Do not add labels like "COUNTER:", "STATEMENT:", "OPENING:", etc. - just speak
102
- your argument
103
- - Focus on content, not formatting or structure
98
+ RESPONSE FORMAT:
99
+ - Speak directly as your assigned role without any labels, prefixes, or announcements
100
+ - Use plain text without markdown formatting (avoid **bold**, *italics*, # headers,
101
+ bullet points)
102
+ - Write in natural conversational style as if speaking live to an audience
103
+ - Do not add labels like "COUNTER:", "STATEMENT:", "OPENING:", etc. - just speak
104
+ your argument
105
+ - Focus on content, not formatting or structure
104
106
 
105
- You will be told your specific role and speaking context for each response."""
107
+ You will be told your specific role and speaking context for each response.
108
+ """).strip()
106
109
 
107
110
  def _create_participant_prompt(
108
111
  self,
@@ -166,18 +169,20 @@ You will be told your specific role and speaking context for each response."""
166
169
  personality, "Speak in your natural style."
167
170
  )
168
171
 
169
- return f"""{base_prompt}
172
+ return dedent(f"""
173
+ {base_prompt}
170
174
 
171
- YOUR ROLE: {role_instruction}
172
- You are not describing what {role_name} would say - you ARE {role_name} speaking
173
- directly. Speak as if you are standing at the podium addressing the audience.
174
- Do not announce your role, add labels, or use prefixes like "COUNTER:" or
175
- "STATEMENT:" - simply speak your argument.
175
+ YOUR ROLE: {role_instruction}
176
+ You are not describing what {role_name} would say - you ARE {role_name} speaking
177
+ directly. Speak as if you are standing at the podium addressing the audience.
178
+ Do not announce your role, add labels, or use prefixes like "COUNTER:" or
179
+ "STATEMENT:" - simply speak your argument.
176
180
 
177
- YOUR SPEAKING STYLE: {style_instruction}
181
+ YOUR SPEAKING STYLE: {style_instruction}
178
182
 
179
- Remember: You are embodying the {role_name} position throughout this debate.
180
- Speak naturally and directly as that person would speak."""
183
+ Remember: You are embodying the {role_name} position throughout this debate.
184
+ Speak naturally and directly as that person would speak.
185
+ """).strip()
181
186
 
182
187
  @staticmethod
183
188
  def get_phase_instruction(phase: DebatePhase) -> str:
@@ -6,7 +6,6 @@ import asyncio
6
6
  import logging
7
7
  import re
8
8
  import time
9
- from collections.abc import Awaitable, Callable
10
9
  from typing import TYPE_CHECKING
11
10
 
12
11
  from dialectus.engine.config.settings import AppConfig
@@ -15,7 +14,7 @@ from dialectus.engine.models.providers import ProviderRateLimitError
15
14
 
16
15
  from .context_builder import ContextBuilder
17
16
  from .models import DebateContext, DebateMessage
18
- from .types import DebatePhase, Position
17
+ from .types import ChunkCallback, DebatePhase, Position
19
18
  from .utils import calculate_max_tokens, query_and_update_cost, trim_incomplete_sentence
20
19
 
21
20
  if TYPE_CHECKING:
@@ -186,7 +185,7 @@ class ResponseHandler:
186
185
  speaker_id: str,
187
186
  format_phase: FormatPhase,
188
187
  context: DebateContext,
189
- chunk_callback: Callable[[str, bool], Awaitable[None]],
188
+ chunk_callback: ChunkCallback,
190
189
  message_id: str | None = None,
191
190
  ) -> DebateMessage:
192
191
  """Get a streaming response from a specific speaker using format phase.
@@ -4,12 +4,17 @@ from __future__ import annotations
4
4
 
5
5
  import logging
6
6
  import uuid
7
- from collections.abc import Awaitable, Callable
8
- from typing import TYPE_CHECKING, Any
7
+ from typing import TYPE_CHECKING
9
8
 
10
9
  from .models import DebateContext, DebateMessage
11
10
  from .response_handler import ResponseHandler
12
- from .types import DebatePhase, Position
11
+ from .types import (
12
+ ChunkCallback,
13
+ DebatePhase,
14
+ MessageEventCallback,
15
+ MessageEventType,
16
+ Position,
17
+ )
13
18
 
14
19
  if TYPE_CHECKING:
15
20
  from formats import DebateFormat, FormatPhase
@@ -96,10 +101,8 @@ class RoundManager:
96
101
  self,
97
102
  format_phase: FormatPhase,
98
103
  context: DebateContext,
99
- message_callback: (
100
- Callable[[str, dict[str, Any]], Awaitable[None]] | None
101
- ) = None,
102
- chunk_callback: Callable[[str, bool], Awaitable[None]] | None = None,
104
+ message_callback: MessageEventCallback | None = None,
105
+ chunk_callback: ChunkCallback | None = None,
103
106
  ) -> list[DebateMessage]:
104
107
  """Conduct a round with streaming callbacks.
105
108
 
@@ -127,7 +130,7 @@ class RoundManager:
127
130
  # Call message_start callback
128
131
  if message_callback:
129
132
  await message_callback(
130
- "message_start",
133
+ MessageEventType.MESSAGE_START,
131
134
  {
132
135
  "message_id": message_id,
133
136
  "speaker_id": speaker_id,
@@ -152,7 +155,7 @@ class RoundManager:
152
155
  # Call message_complete callback
153
156
  if message_callback:
154
157
  await message_callback(
155
- "message_complete",
158
+ MessageEventType.MESSAGE_COMPLETE,
156
159
  {
157
160
  "message_id": message_id,
158
161
  "speaker_id": message.speaker_id,
@@ -0,0 +1,97 @@
1
+ """Shared types and enums for the debate engine."""
2
+
3
+ from collections.abc import Awaitable, Callable
4
+ from enum import Enum
5
+ from typing import Protocol, TypedDict
6
+
7
+
8
+ class PhaseEventType(str, Enum):
9
+ """Event types for phase callbacks."""
10
+
11
+ PHASE_STARTED = "phase_started"
12
+ PHASE_COMPLETED = "phase_completed"
13
+
14
+
15
+ class MessageEventType(str, Enum):
16
+ """Event types for message callbacks."""
17
+
18
+ MESSAGE_START = "message_start"
19
+ MESSAGE_COMPLETE = "message_complete"
20
+
21
+
22
+ class PhaseStartedEventData(TypedDict):
23
+ """Data structure for phase_started event callbacks."""
24
+
25
+ phase: str
26
+ instruction: str
27
+ current_phase: int
28
+ total_phases: int
29
+ progress_percentage: int
30
+
31
+
32
+ class MessageStartEventData(TypedDict):
33
+ """Data structure for message_start event callbacks."""
34
+
35
+ message_id: str
36
+ speaker_id: str
37
+ position: str
38
+ phase: str
39
+ round_number: int
40
+
41
+
42
+ class MessageCompleteEventData(TypedDict):
43
+ """Data structure for message_complete event callbacks."""
44
+
45
+ message_id: str
46
+ speaker_id: str
47
+ position: str
48
+ content: str
49
+ phase: str
50
+ round_number: int
51
+ timestamp: str
52
+ word_count: int
53
+ metadata: dict[str, str]
54
+ cost: float | None
55
+ generation_id: str | None
56
+
57
+
58
+ # Callback protocols with proper type narrowing
59
+ class PhaseEventCallback(Protocol):
60
+ """Protocol for phase event callbacks with type-safe event data."""
61
+
62
+ async def __call__(
63
+ self, event: PhaseEventType, data: PhaseStartedEventData
64
+ ) -> None: ...
65
+
66
+
67
+ class MessageEventCallback(Protocol):
68
+ """Protocol for message event callbacks with type-safe event data."""
69
+
70
+ async def __call__(
71
+ self,
72
+ event: MessageEventType,
73
+ data: MessageStartEventData | MessageCompleteEventData,
74
+ ) -> None: ...
75
+
76
+
77
+ type ChunkCallback = Callable[[str, bool], Awaitable[None]]
78
+
79
+
80
+ class DebatePhase(Enum):
81
+ """Phases of a debate."""
82
+
83
+ SETUP = "setup"
84
+ OPENING = "opening"
85
+ REBUTTAL = "rebuttal"
86
+ CROSS_EXAM = "cross_examination"
87
+ CLOSING = "closing"
88
+ EVALUATION = "evaluation"
89
+ COMPLETED = "completed"
90
+
91
+
92
+ class Position(Enum):
93
+ """Debate positions."""
94
+
95
+ PRO = "pro"
96
+ CON = "con"
97
+ NEUTRAL = "neutral"
@@ -7,6 +7,7 @@ import re
7
7
  import time
8
8
  import uuid
9
9
  from datetime import datetime
10
+ from textwrap import dedent
10
11
 
11
12
  from dialectus.engine.config.settings import SystemConfig
12
13
  from dialectus.engine.debate_engine.models import DebateContext
@@ -245,68 +246,74 @@ parsed programmatically. Be thorough but concise in your reasoning."""
245
246
  example_scores: list[str] = []
246
247
  for criterion in criteria:
247
248
  for side_label in side_labels:
248
- example_scores.append(f""" {{
249
- "criterion": "{criterion}",
250
- "participant": "{side_label}",
251
- "score": 7.5,
252
- "feedback": "specific feedback for {side_label} on {criterion}"
253
- }}""")
249
+ example_scores.append(
250
+ dedent(f"""
251
+ {{
252
+ "criterion": "{criterion}",
253
+ "participant": "{side_label}",
254
+ "score": 7.5,
255
+ "feedback": "specific feedback for {side_label} on {criterion}"
256
+ }}
257
+ """).strip()
258
+ )
254
259
 
255
260
  participants_list = "\n".join(f"- {label}" for label in side_labels)
256
261
  scores_text = ",\n".join(example_scores)
257
262
 
258
- return f"""Please evaluate this debate and provide your judgment
259
- in the following JSON format.
260
-
261
- EVALUATION FOCUS: {random_instruction}
262
- EVALUATION ID: {evaluation_id}
263
-
264
- Please evaluate this debate and provide your judgment
265
- in the following JSON format:
266
-
267
- {{
268
- "winner": "{side_labels[0]} or {side_labels[1]}",
269
- "overall_feedback": "2-3 sentence summary of the debate quality",
270
- "reasoning": "Write a detailed natural language explanation of your
271
- decision. Use complete sentences and paragraphs. Do NOT use structured
272
- data, dictionaries, or lists here - only descriptive text explaining
273
- your thought process.",
274
- "criterion_scores": [
275
- {scores_text}
276
- ]
277
- }}
278
-
279
- EVALUATION CRITERIA: {", ".join(criteria)}
280
-
281
- PARTICIPANTS:
282
- {participants_list}
283
-
284
- CRITICAL INSTRUCTIONS:
285
- - You MUST evaluate BOTH participants on ALL criteria - do not skip
286
- any participant-criterion combinations
287
- - Reference participants by their side labels only
288
- (e.g., "{side_labels[0]}", "{side_labels[1]}")
289
- - The "reasoning" field must contain ONLY natural language text
290
- explaining your decision
291
- - Do NOT put structured data, scores, or dictionaries in the
292
- "reasoning" field
293
- - All numerical scores belong ONLY in the "criterion_scores" array
294
- - Focus on argument quality, evidence, and debate performance
295
- - Provide specific feedback for each participant and criterion
296
- - Your response must include exactly {len(criteria) * len(side_labels)}
297
- criterion_scores entries
298
-
299
- EXAMPLE of correct reasoning field:
300
- "The {side_labels[0]} presented stronger evidence with three concrete
301
- examples, while the {side_labels[1]} relied more on theoretical
302
- arguments. The {side_labels[0]} also did a better job addressing
303
- counterarguments in the rebuttal phase, showing deeper engagement
304
- with the opposing viewpoint."
305
-
306
- DEBATE TRANSCRIPT:
307
- {transcript}
308
-
309
- Provide your evaluation as valid JSON only, no additional text:"""
263
+ return dedent(f"""
264
+ Please evaluate this debate and provide your judgment
265
+ in the following JSON format.
266
+
267
+ EVALUATION FOCUS: {random_instruction}
268
+ EVALUATION ID: {evaluation_id}
269
+
270
+ Please evaluate this debate and provide your judgment
271
+ in the following JSON format:
272
+
273
+ {{
274
+ "winner": "{side_labels[0]} or {side_labels[1]}",
275
+ "overall_feedback": "2-3 sentence summary of the debate quality",
276
+ "reasoning": "Write a detailed natural language explanation of your
277
+ decision. Use complete sentences and paragraphs. Do NOT use structured
278
+ data, dictionaries, or lists here - only descriptive text explaining
279
+ your thought process.",
280
+ "criterion_scores": [
281
+ {scores_text}
282
+ ]
283
+ }}
284
+
285
+ EVALUATION CRITERIA: {", ".join(criteria)}
286
+
287
+ PARTICIPANTS:
288
+ {participants_list}
289
+
290
+ CRITICAL INSTRUCTIONS:
291
+ - You MUST evaluate BOTH participants on ALL criteria - do not skip
292
+ any participant-criterion combinations
293
+ - Reference participants by their side labels only
294
+ (e.g., "{side_labels[0]}", "{side_labels[1]}")
295
+ - The "reasoning" field must contain ONLY natural language text
296
+ explaining your decision
297
+ - Do NOT put structured data, scores, or dictionaries in the
298
+ "reasoning" field
299
+ - All numerical scores belong ONLY in the "criterion_scores" array
300
+ - Focus on argument quality, evidence, and debate performance
301
+ - Provide specific feedback for each participant and criterion
302
+ - Your response must include exactly {len(criteria) * len(side_labels)}
303
+ criterion_scores entries
304
+
305
+ EXAMPLE of correct reasoning field:
306
+ "The {side_labels[0]} presented stronger evidence with three concrete
307
+ examples, while the {side_labels[1]} relied more on theoretical
308
+ arguments. The {side_labels[0]} also did a better job addressing
309
+ counterarguments in the rebuttal phase, showing deeper engagement
310
+ with the opposing viewpoint."
311
+
312
+ DEBATE TRANSCRIPT:
313
+ {transcript}
314
+
315
+ Provide your evaluation as valid JSON only, no additional text:
316
+ """).strip()
310
317
 
311
318
  def _parse_evaluation(
312
319
  self, evaluation: str, context: DebateContext
@@ -3,17 +3,17 @@
3
3
  from __future__ import annotations
4
4
 
5
5
  import logging
6
- from collections.abc import AsyncIterator, Awaitable, Callable
6
+ from collections.abc import AsyncIterator
7
7
  from contextlib import asynccontextmanager
8
8
  from typing import Protocol, cast
9
9
 
10
10
  from dialectus.engine.config.settings import ModelConfig, SystemConfig
11
+ from dialectus.engine.debate_engine.types import ChunkCallback
11
12
  from dialectus.engine.models.base_types import BaseEnhancedModelInfo
12
13
 
13
14
  from .providers.base_model_provider import BaseModelProvider, GenerationMetadata
14
15
  from .providers.providers import ProviderFactory
15
16
 
16
- type ChunkCallback = Callable[[str, bool], Awaitable[None]]
17
17
  type MessageDict = dict[str, str]
18
18
  type MessageList = list[MessageDict]
19
19
  type EnhancedModelRecord = dict[str, object]
@@ -1,7 +1,6 @@
1
1
  from __future__ import annotations
2
2
 
3
3
  from abc import ABC, abstractmethod
4
- from collections.abc import Awaitable, Callable
5
4
  from dataclasses import dataclass
6
5
  from typing import TYPE_CHECKING
7
6
 
@@ -9,6 +8,7 @@ from openai import OpenAI
9
8
 
10
9
  if TYPE_CHECKING:
11
10
  from config.settings import ModelConfig, SystemConfig
11
+ from debate_engine.types import ChunkCallback
12
12
  from models.base_types import BaseEnhancedModelInfo
13
13
 
14
14
 
@@ -68,7 +68,7 @@ class BaseModelProvider(ABC):
68
68
  self,
69
69
  model_config: ModelConfig,
70
70
  messages: list[dict[str, str]],
71
- chunk_callback: Callable[[str, bool], Awaitable[None]],
71
+ chunk_callback: ChunkCallback,
72
72
  **overrides: object,
73
73
  ) -> str:
74
74
  """Generate a streaming response using this provider."""
@@ -92,7 +92,7 @@ class BaseModelProvider(ABC):
92
92
  self,
93
93
  model_config: ModelConfig,
94
94
  messages: list[dict[str, str]],
95
- chunk_callback: Callable[[str, bool], Awaitable[None]],
95
+ chunk_callback: ChunkCallback,
96
96
  **overrides: object,
97
97
  ) -> GenerationMetadata:
98
98
  """Generate a streaming response with full metadata for cost tracking."""
@@ -2,7 +2,6 @@ from __future__ import annotations
2
2
 
3
3
  import json
4
4
  import logging
5
- from collections.abc import Awaitable, Callable
6
5
  from typing import TYPE_CHECKING, cast
7
6
 
8
7
  import httpx
@@ -12,6 +11,7 @@ from .base_model_provider import BaseModelProvider
12
11
 
13
12
  if TYPE_CHECKING:
14
13
  from config.settings import ModelConfig, SystemConfig
14
+ from debate_engine.types import ChunkCallback
15
15
  from models.base_types import BaseEnhancedModelInfo, ModelWeightClass
16
16
  from openai.types.chat import ChatCompletion, ChatCompletionMessageParam
17
17
 
@@ -155,7 +155,7 @@ class OllamaProvider(BaseModelProvider):
155
155
  self,
156
156
  model_config: ModelConfig,
157
157
  messages: list[dict[str, str]],
158
- chunk_callback: Callable[[str, bool], Awaitable[None]],
158
+ chunk_callback: ChunkCallback,
159
159
  **overrides: object,
160
160
  ) -> str:
161
161
  """Generate a streaming response using Ollama."""
@@ -5,7 +5,6 @@ import json
5
5
  import logging
6
6
  import os
7
7
  import time
8
- from collections.abc import Awaitable, Callable
9
8
  from typing import TYPE_CHECKING, ClassVar, cast
10
9
 
11
10
  import httpx
@@ -23,6 +22,7 @@ from .openrouter_generation_types import (
23
22
 
24
23
  if TYPE_CHECKING:
25
24
  from dialectus.engine.config.settings import ModelConfig, SystemConfig
25
+ from dialectus.engine.debate_engine.types import ChunkCallback
26
26
  from dialectus.engine.models.openrouter.openrouter_enhanced_model_info import (
27
27
  OpenRouterEnhancedModelInfo,
28
28
  )
@@ -374,7 +374,7 @@ class OpenRouterProvider(BaseModelProvider):
374
374
  self,
375
375
  model_config: ModelConfig,
376
376
  messages: list[dict[str, str]],
377
- chunk_callback: Callable[[str, bool], Awaitable[None]],
377
+ chunk_callback: ChunkCallback,
378
378
  **overrides: object,
379
379
  ) -> str:
380
380
  """Generate a streaming response using OpenRouter with SSE."""
@@ -3,6 +3,7 @@
3
3
  from __future__ import annotations
4
4
 
5
5
  import logging
6
+ from textwrap import dedent
6
7
 
7
8
  from openai import AsyncOpenAI
8
9
 
@@ -74,22 +75,24 @@ class LLMModerator(BaseModerator):
74
75
  # This format works well with any instruction-following LLM
75
76
  system_prompt = """You are a safety policy expert. Your job is to identify if content violates safety policies, even if the violation is implicit or indirect."""
76
77
 
77
- user_prompt = f"""<start_of_turn>user
78
- {text}
79
- <end_of_turn>
78
+ user_prompt = dedent(f"""
79
+ <start_of_turn>user
80
+ {text}
81
+ <end_of_turn>
80
82
 
81
- **Safety Policies:**
82
- - Harassment: Content that harasses, threatens, or bullies individuals
83
- - Hate Speech: Content promoting hatred toward groups based on identity
84
- - Violence: Content promoting, glorifying, or requesting information about harming others
85
- - Sexual Content: Explicit sexual or adult content
86
- - Dangerous Content: Content promoting dangerous, illegal, or discriminatory activities
83
+ **Safety Policies:**
84
+ - Harassment: Content that harasses, threatens, or bullies individuals
85
+ - Hate Speech: Content promoting hatred toward groups based on identity
86
+ - Violence: Content promoting, glorifying, or requesting information about harming others
87
+ - Sexual Content: Explicit sexual or adult content
88
+ - Dangerous Content: Content promoting dangerous, illegal, or discriminatory activities
87
89
 
88
- **Question:** Does the above user input violate any of these safety policies?
90
+ **Question:** Does the above user input violate any of these safety policies?
89
91
 
90
- **Important:** Content that asks "how to" harm, discriminate, or violate others' rights DOES violate policies, even if phrased as a question.
92
+ **Important:** Content that asks "how to" harm, discriminate, or violate others' rights DOES violate policies, even if phrased as a question.
91
93
 
92
- **Instructions:** Answer ONLY 'Yes' or 'No' first. Answer 'Yes' if the content violates ANY policy above."""
94
+ **Instructions:** Answer ONLY 'Yes' or 'No' first. Answer 'Yes' if the content violates ANY policy above.
95
+ """).strip()
93
96
 
94
97
  try:
95
98
  # Make API call using OpenAI client
@@ -4,7 +4,8 @@ from __future__ import annotations
4
4
 
5
5
  import asyncio
6
6
  import logging
7
- from typing import Iterable
7
+ from collections.abc import Iterable, Mapping
8
+ from typing import Any, Protocol, cast
8
9
 
9
10
  from openai import AsyncOpenAI, RateLimitError
10
11
 
@@ -39,6 +40,42 @@ def _map_categories(category_names: Iterable[str]) -> list[str]:
39
40
  return sorted(mapped)
40
41
 
41
42
 
43
+ class _HasModelDump(Protocol):
44
+ """Protocol for objects with Pydantic v2 model_dump method."""
45
+
46
+ def model_dump(self) -> dict[str, Any]: ...
47
+
48
+
49
+ def _ensure_mapping(data: object) -> dict[str, Any]:
50
+ """Convert Pydantic models to plain dicts for iteration.
51
+
52
+ The OpenAI SDK returns Pydantic v2 models for moderation responses.
53
+ This function normalizes them into plain dictionaries.
54
+ """
55
+ if data is None:
56
+ return {}
57
+
58
+ # Pydantic v2 models (what OpenAI SDK actually returns)
59
+ if hasattr(data, "model_dump"):
60
+ model_dump = cast(_HasModelDump, data).model_dump
61
+ if callable(model_dump):
62
+ return model_dump()
63
+
64
+ # Already a plain dict (used in tests)
65
+ if isinstance(data, dict):
66
+ return cast(dict[str, Any], data)
67
+
68
+ # Generic Mapping fallback (defensive)
69
+ if isinstance(data, Mapping):
70
+ return {str(k): v for k, v in cast(Mapping[Any, Any], data).items()}
71
+
72
+ # If we get here, something unexpected happened
73
+ raise TypeError(
74
+ f"Cannot convert {type(data).__name__} to dict. "
75
+ f"Expected Pydantic model, dict, or Mapping."
76
+ )
77
+
78
+
42
79
  class OpenAIModerator(BaseModerator):
43
80
  """Moderator that uses OpenAI's dedicated moderation endpoint."""
44
81
 
@@ -115,11 +152,11 @@ class OpenAIModerator(BaseModerator):
115
152
 
116
153
  result = results[0]
117
154
  flagged = bool(getattr(result, "flagged", False))
118
- categories_dict = getattr(result, "categories", {}) or {}
119
- category_scores = getattr(result, "category_scores", {}) or {}
155
+ categories_dict = _ensure_mapping(getattr(result, "categories", None))
156
+ category_scores = _ensure_mapping(getattr(result, "category_scores", None))
120
157
 
121
158
  flagged_categories = [
122
- name for name, is_flagged in categories_dict.items() if is_flagged
159
+ name for name, is_flagged in categories_dict.items() if bool(is_flagged)
123
160
  ]
124
161
  mapped_categories = _map_categories(flagged_categories)
125
162
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: dialectus-engine
3
- Version: 0.2.2
3
+ Version: 0.3.0
4
4
  Summary: Dialectus AI debate engine framework
5
5
  Author: Dialectus AI
6
6
  License-Expression: MIT
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "dialectus-engine"
7
- version = "0.2.2"
7
+ version = "0.3.0"
8
8
  description = "Dialectus AI debate engine framework"
9
9
  readme = "README.md"
10
10
  license = "MIT"
@@ -2,11 +2,11 @@
2
2
 
3
3
  from __future__ import annotations
4
4
 
5
+ import asyncio
5
6
  import json
6
7
  import math
7
- import asyncio
8
8
  from types import SimpleNamespace
9
- from typing import Any, cast
9
+ from typing import Any, Protocol, cast
10
10
 
11
11
  import pytest
12
12
 
@@ -19,6 +19,25 @@ from dialectus.engine.moderation.openai_moderator import OpenAIModerator
19
19
  from dialectus.engine.moderation import manager as manager_module
20
20
 
21
21
 
22
+ class _HasModelDump(Protocol):
23
+ """Protocol for objects with Pydantic v2 model_dump method."""
24
+
25
+ def model_dump(self) -> dict[str, Any]: ...
26
+
27
+
28
+ def _normalise_payload(data: object) -> dict[str, Any]:
29
+ """Helper to normalize test data into plain dicts."""
30
+ if isinstance(data, dict):
31
+ return cast(dict[str, Any], data)
32
+ if hasattr(data, "model_dump"):
33
+ return cast(_HasModelDump, data).model_dump()
34
+ try:
35
+ return dict(data) # type: ignore[arg-type]
36
+ except Exception: # pragma: no cover - defensive fallback
37
+ pass
38
+ raise TypeError(f"Unsupported payload type: {type(data)}")
39
+
40
+
22
41
  class FakeModerationResponse:
23
42
  """Lightweight stand-in for the OpenAI moderation response."""
24
43
 
@@ -26,13 +45,13 @@ class FakeModerationResponse:
26
45
  self,
27
46
  *,
28
47
  flagged: bool,
29
- categories: dict[str, bool],
30
- scores: dict[str, float],
48
+ categories: object,
49
+ scores: object,
31
50
  ):
32
51
  payload = {
33
52
  "flagged": flagged,
34
- "categories": categories,
35
- "category_scores": scores,
53
+ "categories": _normalise_payload(categories),
54
+ "category_scores": _normalise_payload(scores),
36
55
  }
37
56
  self.results = [
38
57
  SimpleNamespace(
@@ -180,6 +199,48 @@ def test_openai_moderator_retries_on_rate_limit(
180
199
  assert len(client.requests) == 2
181
200
 
182
201
 
202
+ class _ModelLike:
203
+ """Minimal stand-in for the Pydantic moderation types."""
204
+
205
+ def __init__(self, data: dict[str, Any]) -> None:
206
+ self._data = data
207
+
208
+ def model_dump(self) -> dict[str, Any]:
209
+ return dict(self._data)
210
+
211
+ def __getattr__(self, item: str) -> Any:
212
+ try:
213
+ return self._data[item]
214
+ except KeyError as exc: # pragma: no cover - defensive
215
+ raise AttributeError(item) from exc
216
+
217
+
218
+ def test_openai_moderator_handles_model_objects() -> None:
219
+ """Pydantic-style moderation objects should be normalised automatically."""
220
+ categories_model = _ModelLike({"hate": True, "violence": False})
221
+ scores_model = _ModelLike({"hate": 0.91, "violence": 0.05})
222
+ response = FakeModerationResponse(
223
+ flagged=True,
224
+ categories=categories_model,
225
+ scores=scores_model,
226
+ )
227
+ fake_client = FakeModerationsClient(response)
228
+ moderator = OpenAIModerator(
229
+ api_key="test-key",
230
+ model="omni-moderation-latest",
231
+ timeout=5.0,
232
+ client=make_fake_openai_client(fake_client),
233
+ )
234
+
235
+ result = asyncio.run(
236
+ moderator.moderate("Edge case flagged by typed moderation payloads.")
237
+ )
238
+
239
+ assert result.is_safe is False
240
+ assert result.categories == ["hate_speech"]
241
+ assert math.isclose(result.confidence, 0.91, rel_tol=1e-3)
242
+
243
+
183
244
  def test_manager_initialises_openai_provider(monkeypatch: pytest.MonkeyPatch) -> None:
184
245
  """Manager should build the specialised OpenAI moderator when requested."""
185
246
  captured: dict[str, object] = {}
@@ -1,23 +0,0 @@
1
- """Shared types and enums for the debate engine."""
2
-
3
- from enum import Enum
4
-
5
-
6
- class DebatePhase(Enum):
7
- """Phases of a debate."""
8
-
9
- SETUP = "setup"
10
- OPENING = "opening"
11
- REBUTTAL = "rebuttal"
12
- CROSS_EXAM = "cross_examination"
13
- CLOSING = "closing"
14
- EVALUATION = "evaluation"
15
- COMPLETED = "completed"
16
-
17
-
18
- class Position(Enum):
19
- """Debate positions."""
20
-
21
- PRO = "pro"
22
- CON = "con"
23
- NEUTRAL = "neutral"