Skip to content

Commit e931cf0

Browse files
waleedlatif1claude
andcommitted
fix(azure): unify credential passing pattern across all LLM handlers
- Pass all provider credentials unconditionally in router, evaluator (matching agent pattern) - Remove conditional if-branching on providerId for credential fields - Thread workspaceId through guardrails → hallucination validator for BYOK key resolution - Remove getApiKey() from hallucination validator, let executeProviderRequest handle it - Resolve vertex OAuth credentials in hallucination validator matching agent handler pattern Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 92b2253 commit e931cf0

File tree

4 files changed

+34
-64
lines changed

4 files changed

+34
-64
lines changed

apps/sim/app/api/guardrails/validate/route.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export async function POST(request: NextRequest) {
3232
bedrockSecretKey,
3333
bedrockRegion,
3434
workflowId,
35+
workspaceId,
3536
piiEntityTypes,
3637
piiMode,
3738
piiLanguage,
@@ -129,6 +130,7 @@ export async function POST(request: NextRequest) {
129130
bedrockRegion,
130131
},
131132
workflowId,
133+
workspaceId,
132134
piiEntityTypes,
133135
piiMode,
134136
piiLanguage,
@@ -207,6 +209,7 @@ async function executeValidation(
207209
bedrockRegion?: string
208210
},
209211
workflowId: string | undefined,
212+
workspaceId: string | undefined,
210213
piiEntityTypes: string[] | undefined,
211214
piiMode: string | undefined,
212215
piiLanguage: string | undefined,
@@ -249,6 +252,7 @@ async function executeValidation(
249252
apiKey,
250253
providerCredentials,
251254
workflowId,
255+
workspaceId,
252256
requestId,
253257
})
254258
}

apps/sim/executor/handlers/evaluator/evaluator-handler.ts

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -121,26 +121,17 @@ export class EvaluatorBlockHandler implements BlockHandler {
121121

122122
temperature: EVALUATOR.DEFAULT_TEMPERATURE,
123123
apiKey: finalApiKey,
124+
azureEndpoint: inputs.azureEndpoint,
125+
azureApiVersion: inputs.azureApiVersion,
126+
vertexProject: evaluatorConfig.vertexProject,
127+
vertexLocation: evaluatorConfig.vertexLocation,
128+
bedrockAccessKeyId: evaluatorConfig.bedrockAccessKeyId,
129+
bedrockSecretKey: evaluatorConfig.bedrockSecretKey,
130+
bedrockRegion: evaluatorConfig.bedrockRegion,
124131
workflowId: ctx.workflowId,
125132
workspaceId: ctx.workspaceId,
126133
}
127134

128-
if (providerId === 'vertex') {
129-
providerRequest.vertexProject = evaluatorConfig.vertexProject
130-
providerRequest.vertexLocation = evaluatorConfig.vertexLocation
131-
}
132-
133-
if (providerId === 'azure-openai' || providerId === 'azure-anthropic') {
134-
providerRequest.azureEndpoint = inputs.azureEndpoint
135-
providerRequest.azureApiVersion = inputs.azureApiVersion
136-
}
137-
138-
if (providerId === 'bedrock') {
139-
providerRequest.bedrockAccessKeyId = evaluatorConfig.bedrockAccessKeyId
140-
providerRequest.bedrockSecretKey = evaluatorConfig.bedrockSecretKey
141-
providerRequest.bedrockRegion = evaluatorConfig.bedrockRegion
142-
}
143-
144135
const response = await fetch(url.toString(), {
145136
method: 'POST',
146137
headers: await buildAuthHeaders(),

apps/sim/executor/handlers/router/router-handler.ts

Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -96,26 +96,17 @@ export class RouterBlockHandler implements BlockHandler {
9696
context: JSON.stringify(messages),
9797
temperature: ROUTER.INFERENCE_TEMPERATURE,
9898
apiKey: finalApiKey,
99+
azureEndpoint: inputs.azureEndpoint,
100+
azureApiVersion: inputs.azureApiVersion,
101+
vertexProject: routerConfig.vertexProject,
102+
vertexLocation: routerConfig.vertexLocation,
103+
bedrockAccessKeyId: routerConfig.bedrockAccessKeyId,
104+
bedrockSecretKey: routerConfig.bedrockSecretKey,
105+
bedrockRegion: routerConfig.bedrockRegion,
99106
workflowId: ctx.workflowId,
100107
workspaceId: ctx.workspaceId,
101108
}
102109

103-
if (providerId === 'vertex') {
104-
providerRequest.vertexProject = routerConfig.vertexProject
105-
providerRequest.vertexLocation = routerConfig.vertexLocation
106-
}
107-
108-
if (providerId === 'azure-openai' || providerId === 'azure-anthropic') {
109-
providerRequest.azureEndpoint = inputs.azureEndpoint
110-
providerRequest.azureApiVersion = inputs.azureApiVersion
111-
}
112-
113-
if (providerId === 'bedrock') {
114-
providerRequest.bedrockAccessKeyId = routerConfig.bedrockAccessKeyId
115-
providerRequest.bedrockSecretKey = routerConfig.bedrockSecretKey
116-
providerRequest.bedrockRegion = routerConfig.bedrockRegion
117-
}
118-
119110
const response = await fetch(url.toString(), {
120111
method: 'POST',
121112
headers: await buildAuthHeaders(),
@@ -234,6 +225,13 @@ export class RouterBlockHandler implements BlockHandler {
234225
context: JSON.stringify(messages),
235226
temperature: ROUTER.INFERENCE_TEMPERATURE,
236227
apiKey: finalApiKey,
228+
azureEndpoint: inputs.azureEndpoint,
229+
azureApiVersion: inputs.azureApiVersion,
230+
vertexProject: routerConfig.vertexProject,
231+
vertexLocation: routerConfig.vertexLocation,
232+
bedrockAccessKeyId: routerConfig.bedrockAccessKeyId,
233+
bedrockSecretKey: routerConfig.bedrockSecretKey,
234+
bedrockRegion: routerConfig.bedrockRegion,
237235
workflowId: ctx.workflowId,
238236
workspaceId: ctx.workspaceId,
239237
responseFormat: {
@@ -257,22 +255,6 @@ export class RouterBlockHandler implements BlockHandler {
257255
},
258256
}
259257

260-
if (providerId === 'vertex') {
261-
providerRequest.vertexProject = routerConfig.vertexProject
262-
providerRequest.vertexLocation = routerConfig.vertexLocation
263-
}
264-
265-
if (providerId === 'azure-openai' || providerId === 'azure-anthropic') {
266-
providerRequest.azureEndpoint = inputs.azureEndpoint
267-
providerRequest.azureApiVersion = inputs.azureApiVersion
268-
}
269-
270-
if (providerId === 'bedrock') {
271-
providerRequest.bedrockAccessKeyId = routerConfig.bedrockAccessKeyId
272-
providerRequest.bedrockSecretKey = routerConfig.bedrockSecretKey
273-
providerRequest.bedrockRegion = routerConfig.bedrockRegion
274-
}
275-
276258
const response = await fetch(url.toString(), {
277259
method: 'POST',
278260
headers: await buildAuthHeaders(),

apps/sim/lib/guardrails/validate_hallucination.ts

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { eq } from 'drizzle-orm'
55
import { getBaseUrl } from '@/lib/core/utils/urls'
66
import { refreshTokenIfNeeded } from '@/app/api/auth/oauth/utils'
77
import { executeProviderRequest } from '@/providers'
8-
import { getApiKey, getProviderFromModel } from '@/providers/utils'
8+
import { getProviderFromModel } from '@/providers/utils'
99

1010
const logger = createLogger('HallucinationValidator')
1111

@@ -34,6 +34,7 @@ export interface HallucinationValidationInput {
3434
bedrockRegion?: string
3535
}
3636
workflowId?: string
37+
workspaceId?: string
3738
requestId: string
3839
}
3940

@@ -103,8 +104,9 @@ async function scoreHallucinationWithLLM(
103104
userInput: string,
104105
ragContext: string[],
105106
model: string,
106-
apiKey: string,
107+
apiKey: string | undefined,
107108
providerCredentials: HallucinationValidationInput['providerCredentials'],
109+
workspaceId: string | undefined,
108110
requestId: string
109111
): Promise<{ score: number; reasoning: string }> {
110112
try {
@@ -142,8 +144,7 @@ Evaluate the consistency and provide your score and reasoning in JSON format.`
142144

143145
const providerId = getProviderFromModel(model)
144146

145-
// Resolve Vertex AI OAuth credential to access token if needed
146-
let finalApiKey = apiKey
147+
let finalApiKey: string | undefined = apiKey
147148
if (providerId === 'vertex' && providerCredentials?.vertexCredential) {
148149
const credential = await db.query.account.findFirst({
149150
where: eq(account.id, providerCredentials.vertexCredential),
@@ -178,6 +179,7 @@ Evaluate the consistency and provide your score and reasoning in JSON format.`
178179
bedrockAccessKeyId: providerCredentials?.bedrockAccessKeyId,
179180
bedrockSecretKey: providerCredentials?.bedrockSecretKey,
180181
bedrockRegion: providerCredentials?.bedrockRegion,
182+
workspaceId,
181183
})
182184

183185
if (response instanceof ReadableStream || ('stream' in response && 'execution' in response)) {
@@ -233,6 +235,7 @@ export async function validateHallucination(
233235
apiKey,
234236
providerCredentials,
235237
workflowId,
238+
workspaceId,
236239
requestId,
237240
} = input
238241

@@ -251,17 +254,6 @@ export async function validateHallucination(
251254
}
252255
}
253256

254-
let finalApiKey: string
255-
try {
256-
const providerId = getProviderFromModel(model)
257-
finalApiKey = getApiKey(providerId, model, apiKey)
258-
} catch (error: any) {
259-
return {
260-
passed: false,
261-
error: `API key error: ${error.message}`,
262-
}
263-
}
264-
265257
// Step 1: Query knowledge base with RAG
266258
const ragContext = await queryKnowledgeBase(
267259
knowledgeBaseId,
@@ -283,8 +275,9 @@ export async function validateHallucination(
283275
userInput,
284276
ragContext,
285277
model,
286-
finalApiKey,
278+
apiKey,
287279
providerCredentials,
280+
workspaceId,
288281
requestId
289282
)
290283

0 commit comments

Comments
 (0)